I'm working on a program which requires me to read in a file of input and organize it by calling a few functions. The first function I'm writing breaks up the input and assigns it to the appropriate array. For some reason what I keep getting back is random values. Any help would be appreciated here is my code and directions given to us if needed. Thank You in advance.
http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm10.htm

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void dissectCustLine( char *, char *, char *, char *, char *, char * );

int main()
{
    ifstream inFile;
    char input[80];
    char name[16];
    char addr[16];
    char telephone[16];
    char carinfo[16];
    char payment[16];
    
    inFile.open( "custfile.txt" );
    
    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    inFile.getline( input, 80 );
    while ( inFile )
    {
    dissectCustLine( &input[80], &name[16], &addr[16], &telephone[16], &carinfo[16], &payment[16] );
    cout << name << endl;
    inFile.getline( input, 80 );
    }     
    
    system ("pause");
    return 0;
}
/*********************************************************/
void dissectCustLine ( char *i, char *n, char *a, char *t, char *c, char *p )
{
     n = strncpy(n, i, 15);
     *(n+15) = '\0';
     
}

any suggestions? I'm really stuck for some reason.

Try changing your function call (line 29) to this:

dissectCustLine( input, name, addr, telephone, carinfo, payment );

You want to pass the function a pointer to the START of each array, which is index 0. Right now you are passing pointers to one character PAST THE END of the array (index 16 - legal indexes would be 0 to 15). Index 0 is simply the array itself, so this address:

&input[0]

is the same address as this address:

input

so in the function call, I left off the indexes.

awesome that fixed it i'm sure ill be back with more problems lol

I have to write this function below that make sure the street and suffix start with a upper case and the rest are lower case. How would I go about doing that one?

void fixAddress( char * );

This function should edit the street name and suffix by making sure that each begins with an upper case letter, and that the rest are lower case.

It takes one string argument: the customer street address string. This string will initially contain the unedited street address string and will also be used to pass back the edited street address string.

HINT: Take advantage of the fact that the fields are separated by a space.

I have to write this function below that make sure the street and suffix start with a upper case and the rest are lower case. How would I go about doing that one?

void fixAddress( char * );

This function should edit the street name and suffix by making sure that each begins with an upper case letter, and that the rest are lower case.

It takes one string argument: the customer street address string. This string will initially contain the unedited street address string and will also be used to pass back the edited street address string.

HINT: Take advantage of the fact that the fields are separated by a space.

The cctype library has several functions that test characters and/or converts them.

http://www.cplusplus.com/reference/clibrary/cctype/

In particular, isupper and islower to test upper/lower case, and toupper and tolower can be used to convert to upper or lower case.

All these functions work on characters, not strings, so you'd need to isolate individual characters.

When he says take advantage of the fact that the fields have a space in between them, he is probably meaning for you to use the strtok function, or for you to write your own.

strtok works by taking a string and a delimiter and every time it sees the delimiter it returns the string up to that point. Look it up on the man pages for a better description of it. You might try writing your own strtok function though.

Also, stop using strcpy or strncpy. Those are just shitty functions. So often I had trouble at school because those functions aren't very useful. Use memcpy instead. strcpy is basically an assignment function. memcpy will copy one memory location to another.

Well this is where I am at with this function so far and it ends up making everything capitalized when its only suppose to be the first letter capitialized and the rest lowercase. Suggestions?

void fixAddress ( char *a )
{
     char *fixPtr;
     char ch;
     fixPtr = strchr(a,ch >= 'a' && ch <= 'z');
     for (char *p = a; p!= fixPtr; p++)
     {
         *p=toupper(*p);
     }
     for (char *m = fixPtr+1; *m != 0; m++)
     {
         *m = tolower(*m);
     }
     
}

So this should not be in a loop?

for (char *p = a; p!= fixPtr; p++)
     {
         *p=toupper(*p);
     }

Why not do something like:

Set first letter to uppercase.
Rest to lowercase

with

void fixAddress ( char *a )
{
  a[0] = toupper(a[0]);
     for (int i = 0; i < strlen(a); i++)
     {
         a[i] = tolower(a[i]);
     }
     
}

Which only works if a is null-terminated of course, but I'm guessing it is.

Ok I did that and I know get everything lowercase.

err, sorry

Change that for loop, init part to int i = 1, ofc. xD

like this:

void fixAddress ( char *a )
{
  a[0] = toupper(a[0]);
     for (int i = 1; i < strlen(a); i++)
     {
         a[i] = tolower(a[i]);
     }
     
}

PS: I take it that you do understand how this works right?

Ya I get what I'm doing for some reason I keep getting all lower case, I been playing around with it and just can't see what the issue would be.

Err, post your complete code? I'm getting curious now. xD

This is where am I at. This program is giving me a very hard time for some odd reason any help would be great

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void dissectCustLine( char *, char *, char *, char *, char *, char * );
void fixName( char * );
void fixAddress( char * );
void fixPhone ( char * );


int main()
{
    ifstream inFile;
    char input[80];
    char name[15];
    char addr[18];
    char telephone[12];
    char carinfo[21];
    char payment[16];
    char output[80];
    
    inFile.open( "custfile.txt" );
    
    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    inFile.getline( input, 80 );
    while ( inFile )
    {
    dissectCustLine( input, name, addr, telephone, carinfo, payment );
    fixName( name );
    fixAddress ( addr );
    fixPhone ( telephone );
    
    inFile.getline( input, 80 );
    }     
    system ("pause");
    return 0;
}
/*********************************************************/
void dissectCustLine ( char *i, char *n, char *a, char *t, char *c, char *p )
{
    strncpy(n, i, 15);
    n[15] = '\0';
    
    strncpy(a, i+15, 18);
    a[18] = '\0';
    
    strncpy(t, i+33, 12);
    t[12] = '\0';
    
    strncpy(c, i+45, 21);
    c[21] = '\0';
    
    strncpy(p, i+67, 8);
    p[8] = '\0';
}
/*********************************************************/
void fixName ( char *n )
{
     char temp[80];
     char *spacePtr;
     spacePtr = strchr(n, ',');
     if ( spacePtr != 0 )
     {
     strcpy(temp,spacePtr+1);
     strcat(temp, ",");
     *spacePtr = '\0';
     strcat(temp, n);
     strcpy(n, temp);
     }
}
/******************************************/
void fixAddress ( char *a )
{  
   a[0] = toupper(a[0]);
        for (int i = 1; i < strlen(a); i++)
        {
            a[i] = tolower(a[i]);
        }
}
  /*char *fixPtr;
     
     fixPtr = strchr( a, ' ' );
     for (char *p = a; p != fixPtr; p++)
     {
         *p = toupper(*p);
     }
     for (char *m = fixPtr+1; *m != 0; m++)
     {
         *m = toupper(*m);
     }*/     
/********************************************************/
void fixPhone ( char *t)
{
}

Your file would be helpful as well, just to make sure.

Nevermind, grabbed it from the site:

JOHN MCCARTHY  3245 HILLCREST DR 8157359091  Mercedez_bEnz, S500   85800.00
Levy, MATTHEW  10 Shady ln       3126883568  chevrolet, MALIBU     16750.00
james lancaster1206 Hillgrove Ave9017492475  BUICK, lesabre        24230.00
Gates, Dennis  2066 Woodland st  2136548521  AM_GENERAL, HUMMER_4DR94529.00
Randy kraft    1721 Grant Hwy    4093651514  linCOLn, TOWN CAR     48610.00
seymore, BETH  3001 BLACKHAWK ST 3076567465  PONTIAC, BONNEVILLE   32560.00
Forest Pierce  24 Wilson Rd      2178524582  cadillac, escalade    50285.00
Knoll, Bradley 3500 Brayton Place7012689511  Mercury, Cougar       16300.00
Joyce More     23 Greenwood dr   7486308523  TOYOTA, AVALON        30405.00
Kingston, shari5110 glidden rd   5202314583  DODGE, INTREPID       24975.00
JOSEPH HUMPHREY19 sitka ln       2127651591  VOLKSWAGEN, beetle    16850.00
clark, Aaron   4381 first st     9158659899  porsche, 911          81500.00
Gail Holtman   3311 Taylor rd    2136754142  volvo, S80            48750.00
Spencer, Teresa56 lucinda Ave    3126533653  BUick, regal          28000.00

Oh I'm so sorry! I thought the addresses started with a alphabetic char (like Shady In 10) But they don't! Sorry sorry sorry.

Here's your fixed function, study it a bit, but not too long: it's C, not C++ we're coding here. :( You're better of using std::string.

void fixAddress ( char *a ) {
    //find first alphabetic char
    //this loops while *a != alphabetic char or we reached the end of the array.
    while(*a != NULL){
        if(isalpha(*a)) break;
        a++;
    }

    //make the first alphabetic char uppercase
    *a = toupper(*a);

    //go to the next char
    a++;
    //loops till the end of the array, sets every char to lowercase
    while(*a != NULL){
        *a = tolower(*a);
        a++;
    }
}

you need to #include <cctype> for the isalpha() function. :)

alright awesome that worked for the name of the street what should I do to fix the suffix?

Depends on what there is to fix with the suffix. :)

On a side note: 300! Ahem. *clears throat*
TONIGHT
WE DINE
AT DANIWEB!

I just need to capitialize the first letter and lowercase the rest again.

Ah, well I'll leave that exercise up for you now. You should be able to figure that out, if not, I won't be helping you for at least 8 hours: bedtime.

Good luck with the suffix and the phone numbers!

Alright man its much appreciated take it easy.

If anyone else has any suggestions on how to capitalize that be great.

I also have to write a function that adds hyphens to the phone numbers so it goes from 4445556666 to 444-555-6666.

This is where I am at with this.

void fixPhone ( char *t)
{
    *t = *t+3;
    cout << "-";
}

Does it only need to be in the output or also in the data itself?

Cout doesn't alter the data itself.

Consider what you want:

Skip 3 numbers
insert a hyphen
Skip 3 numbers
insert a hyphen

So, the string grows, you need to make the telephone number string bigger at the start and then alter it, else you'll get memory errors.

Try writing an insert function. (There are other ways to do it of course)

str.insert(3,1,'-'); is htat how the insert function would work because I am getting a compiler error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.