C++ question

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Is there some function similar to getline() that can be used to descrimnate when to stop reading a line of input off the istream so that if there is more than one data type on the line you can read them in seperately?
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Never heard of getc(), gets() though. My line is split into string literals and then an int literal, depending on what the user inputs though there is no way to determine how many words the string literal is so I need some way to keep taking string input in until I reach numerical digits. Since, anything can be a string, I can't use istream flags for testing. Anyone have any ideas?
 

PhaZe

Platinum Member
Dec 13, 1999
2,880
0
76
pff jmaster=nef.


I dont think if this will help but I have this link that contains alot of c++ material
here
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
Mears:

Can you explain a little more detail on what you need to do?
Are you coding this in "C" or "C++"?
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
I checked out the site but couldn't find any info. What I'm trying to do is something like this:

Say you want the user to input the name of a basketball player and how many points they scored last year. They could input something like:

John Smith 255
Bill S Spears 344
J W Grant 137

or any number of partial names the player could have. I just need to find a way to keep reading in to a string until I hit numerical data. I can't just do a certain number of cin >> "string variable";'s either since I don't know how many parts of the name the individual will have.
 

littleprince

Golden Member
Jan 4, 2001
1,339
1
81
i'm way to tired...
but read them in to arrays...
check if strintok...
if ok
convert to int

well, thats the first thing that comes to my mind
but i'm way to tired
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
I haven't learned arrays yet, so there must be some other way of doing this.
 

BowDown

Banned
Jun 2, 2000
2,197
0
0
scan it character by character and check the ASCII value to see if it's outside of the character range... If it's not a character in your example it has to be an int.
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
ok, if I read in character by character is there a way to convert and combine the characters that are part of the string name together and then convert and combine the characters that are part of the number to an int value?
 

Freeze

Senior member
Oct 15, 1999
681
0
0
well, if you really wanted to, you could blindly read the string into an array and then check to see if one of the characters is less than 'A' (ascii A) and that it is not a space. All characters are above 0x41 in the ascii table, so you could do it that way. BTW, a space is 0x20. just a thought.
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
But how would I seperate the two type of literals so that the numbers are copied to a int variable and the names copied to a string variable?
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
There are 2 options:
The easiest would be to make it so that the number comes first, and then the name. Have it
cin the first part into an integer, then just cin again to get the rest of the input into a string.

If the number must come last, then simply read the entire line, then write a parsing function which copies everything into a new string until it reaches a number, then copy everything including and after that point into a new string (then copy it into an integer usint atoi() if you want).


I don't think there's any pre-written function to do this. scanf() could get close, but it usually uses space to split up words, so if the name was more than one word, it would have problems.
 

Turkey

Senior member
Jan 10, 2000
839
0
0
You can read everything into a string using getline. Then use the string functions to parse out the last value in the space-delimited string. There's a function called atoi that converts from a C string to an int if the value is an int and returns a defined error value if not. So, use the c_str() function of the string class to get the C string representation of the int, pass it to atoi, and you're done.

atoi function reference
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Cool, looks like I'll be using atoi() then. I am curious if there is some other way to do this since the atoi() function isn't listed in the book my class uses.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
If you're looking to say, "split" a string based on some delimeter, you could do something like this:

int len = strlen(input);
char *p = input[len];

char *c, d*;
if ((c = strchr(input, delimeter)) != NULL)
{
d = c;
d++;
*c = '\0';
}

now input contains the string to the delimeter, and d is the string after.. Of course, I haven't tested this, so don't assume a copy/paste solution. Just trying to get the idea across...


 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Ok...I tried this test using atoi():

#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <math.h>
int main()
{
string x = 454;
int y = atoi(x);
cout << y;
cin >> x; //used because my dos window automatically closes if I don't put it in
}

Upon compiling I received these errors:

7 c:\docume~1\admini~1\mydocu~1\untitl~4.cpp
conversion from `int' to non-scalar type `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >' requested

8 c:\docume~1\admini~1\mydocu~1\untitl~4.cpp
cannot convert `x' from type `string' to type `const char *'


Anyone have any ideas?

 

Pretender

Banned
Mar 14, 2000
7,192
0
0
Ah, I didn't know what type of strings you were using. atoi() uses regular strings (char *). I don't know how to do with with string class strings.



BTW, to use atoi, you'd do something like this:

char *myString = &quot;4528&quot;;
int x;
x = atoi(myString);
// x will now == 4528
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
If you use &quot;regular&quot; strings is their alternatives for string class manipulations?
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Ok...I had an idea. I could use a for loop and use peek() to find the point where a numerical digit is found. After I find that is there a function that I could use to read in the stuff prior to that position into a string?
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Ok, I think I finally figured it out. I think if I use a combination of peek() along with
seekg() I'll get it.