C++ help

Motoxrdude

Active Member
I missed the last couple of days and I am playing catch up. Right now I need to make a program that reads a file, assigns a couple variables and the display the variables.

So I need it to read two words (space in between) from the file and then stop at an ",". How in the world do I get it to skip a white space but not a certain character like ","? If I just do "inFile >> stringVar" it will stop at the first white space, but if i do "getline(inFile, stringVar)" it will read the entire line.
 
Try this:
Code:
//declarations
char a[81];
char* word1, word2;

//read string
filein.getline(a,80);

//get the first word
for (int i=0;a[i]!=' ';i++)
      word1[i]=a[i];

i++; // advance to the position of the second word

//get the second word
for (int j=0;a[i]!=' ';j++) {
      word2[j]=a[i]; i++;
      }

If the words are separated by other characters like comma or whatever, just replace in the for a!=<the character between apostrophes>
 
Last edited:
Thanks for the input. That is kinda what I want to do, but not really.

I have an input file and the file reads
Code:
Santa Rosa, CA, 95403

I want to set "Santa Rosa" as a variable, CA as a variable and 95403 as a variable. The only problem is that the city could possibly change to only one word, such as "Sacremento" or something like that. So basically i need it to read all the way up to the comma and then stop.
 
Ok nevermind, I figured it out. Turns out we could modify the input file so i put the city, state and zip on seperate lines. Thanks guys :P
 
Thanks for the input. That is kinda what I want to do, but not really.

I have an input file and the file reads
Code:
Santa Rosa, CA, 95403

I want to set "Santa Rosa" as a variable, CA as a variable and 95403 as a variable. The only problem is that the city could possibly change to only one word, such as "Sacremento" or something like that. So basically i need it to read all the way up to the comma and then stop.

In that case, you could use the comma as a separator, than skip until the next comma and get the number.
 
How would I go about doing that?

//declarations
char a[81];
char* word1, word2;

//read string
filein.getline(a,80);

//get the first word
for (int i=0;a!=', ';i++)
word1=a;

i++; // advance to the position of the second word

//get the second word
for (int j=0;a!=', ';j++) {
word2[j]=a; i++;
}

i++;

//get third word
for (int j=0;a!=' ';j++) {
word3[j]=a; i++;
}

Do it like that?
 
Why get the second word when you don't need it?
Code:
//declarations
char a[81];
char* word1, word3;

//read string
filein.getline(a,80);

//get the first word
for (int i=0;a[i]!=',';i++)
word1[i]=a[i];

i=i+2; // advance to the position of the second word (skip the space)

//skip second word
while (a[i]!=',') i++;

i=i+2;

//get third word
for (int j=0;i<=strlen(a);j++) { // or you could put a[i]!=NULL in the condition since any string ends with a null character
word3[j]=a[i]; i++;
}

And then if you need to turn a string into a int variable (i recommend you float since it is bigger than 32,000) use
atoi(<string>); (or atof(<string>); for float)
 
Last edited:
(i recommend you float since it is bigger than 32,000)
Unless this is running on a 16-bit or 8-bit machine an unsigned integer can store values up to 4.2 someodd billion (~2 billion for signed).

Also, getch can be your friend :P
 
Why get the second word when you don't need it?
Code:
//declarations
char a[81];
char* word1, word3;

//read string
filein.getline(a,80);

//get the first word
for (int i=0;a[i]!=',';i++)
word1[i]=a[i];

i=i+2; // advance to the position of the second word (skip the space)

//skip second word
while (a[i]!=',') i++;

i=i+2;

//get third word
for (int j=0;i<=strlen(a);j++) { // or you could put a[i]!=NULL in the condition since any string ends with a null character
word3[j]=a[i]; i++;
}

And then if you need to turn a string into a int variable (i recommend you float since it is bigger than 32,000) use
atoi(<string>); (or atof(<string>); for float)

Oh okay, I didn't know the second word needed to be skipped.
 
Back
Top