-
I'm not at a computer right now but i see a couple of things.
1. I personally would break each stage of the input up to make it easier, and consequently, easier to track down your bugs.
2. Cin.get() is unecessary if the user input is wrong, and is likely having a negative affect on the conditions of your while loop
-
Strlen is for c style char arrays, you want .length()
-
Oke,
the problem still exist.
When I do this
Code:
if (length(home_team)==0 && length(away_team)==0 && length(score_away_team)!=0 && length(score_home_team)==0) {
I get this a message :
C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp |20|error: 'length' was not declared in this scope|
And when I do this :
Code:
if (home_team.length()==0
The same error appears.
Roelof
-
-
Yep,
I included string and cstring.
Roelof
-
Ok then should be correct.
But if it's not working repost the code.
-
Oke,
I see the problem now.
If I do if (home_team.length=0) It's oke
But when I do if(home_team.length=0 && away_team.length=0) I get this message:
C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp |20|error: invalid use of member (did you forget the '&' ?)|
So i think and rewriet it so every input get seperate check.
Roelof
-
If you are using if(home_team.length()=0) it should be if(home_team.length()==0)
Jim
-
Oke,
Does anyone know how I can check if a input only contains numbers ?
Roelof
Edit 1 :
The only thing I can think is to make a loop and check everything in the string seperate.
with converting yoiu have the problem that if the string contains 0 converting give the same answer if string contains a.
-
Why bother converting from a string to an int? Strings are obviously more flexible than ints for user input. With strings a user can enter "1", "one", "One", or "ONE" which all mean the same thing. If the string matches any of those, then assign the value 1 to an integer.
-
Hello,
There is no problem.
The problem is the input of score_home_team and score_away_team.
That must be numbers because I use them for calculating of the total points a team made and the total points other teams made against this team.
Also I have to decide which team has won the game.
Roelof
-
-
Sorry,
Im a noob in C++.
What do you mean by ^[/d]+$
Roelof
-
Hello,
I have googled but this seems to be a print solution.
I was looking for input validation that the string only contains numbers.
Roelof
Edit :
can this be a good solution:
Code:
int myNumber = 0;
while (true) {
cout << "Please enter a valid number: ";
getline(cin, input);
// This code converts from string to number safely.
stringstream myStream(input);
if (myStream >> myNumber)
break;
cout << "Invalid number, please try again" << endl;
}
cout << "You entered: " << myNumber << endl << endl;
can someone tell me what mystream >> mynumber means ?
-
Nobody.
Is there a way i can use a loop and take all the characters one for one for a check if it's a number ?
Roelof