hi!!!! i'd like to ask if anyone knows how could i possibly use a string as a number...i mean:
i've got something like cin>>word;
but if that word is a number(like 1,2,3... etc) i have to use that number for going in that line.
if user types ''hello'' its ok.but if he types ''4'' i heve to use that number for going at ARRAY[4].
HOOOOOOOOOOOOOW????
(I am using a vector in which the user types words.if he types a number i have to go at that line of vector and begin wgiting all over again from line 4[example])
int i;
string str;//or char str[100]
if(cin >> i){
cout << i << " is a number!";
}else{
cin.clear();
cin >> str;
cout << str << " is a word!";
}
This works because when you try to get an integer from cin, if there is no integer in the stream, a fail flag is set. stream can be converted to a boolean that represents this flag. (otherwise you'd write if( (cin>>i).fail() ){)
well i didn't get that 100% :S :/
i think it can't help me cause' me project is a word editor.
first of all i'm using a vector.
i have to make a prog that takes words from the user (cin>>word).
i have olso a counter that points in which line the user is
(example) 1>hello
2>word
3>how are u?
4>i want to go to line 2 and begin all over again.so line 2 is going to be replaced just like 3,4,5...
5>:2
2>_
this is what i mean..so how can i use the word(string) so that i can make counter=word when word is number?sorry if i confused u.. :/ i think that i have to put all these in an IF! if (word is number)
{counter=word,delete everything else below line 2 in my vector;}
(firstly, user enters a string/number and presses [enter], therefore that string/number is in the input stream)
line 3: Here we try to read an integer from the stream. If there is one, it is read and stored in variable 'i' and we go to line 4. Otherwise, whatever was in the stream remains there and we go to line 6.
line 6: We clear the error flags so that we can do the >> again.
line 7: We read whatever was in the stream into a string. If we reached this point, it isn't a number. This will not make the user enter anything again. This will retrieve the string that our previous >> operation left in the stream.