Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (psymon):

Another question came up. C++ if-else statements

OpenStudy (psymon):

So, basically I only want the program to recognize certain values. So how I tell the program to consider EVERYTHING except what the values I'm looking for to be considered invalid? Like for example, if the programs prompts for input and I want only numbers to be recognized, how do I tell the program to respond to ANY other value with cout << "Invalid input << endl; I guess I dont know how to format the proceeding if-else statement.

OpenStudy (psymon):

cout << "invalid input" << endl; Forgot other quotation, whoops.

OpenStudy (e.mccormick):

You would need to take in the value as a string then evaluate it. If it ends up not being what you need, you toss an error at the user.

OpenStudy (e.mccormick):

Here is an easy method to test with: http://www.cplusplus.com/forum/articles/6046/

OpenStudy (psymon):

We aren't allowed to. We can only use what's been covered in lecture. So Im looking at this switch (expression) stuff. Is there anyway that might be able to modified to do it? I can see how it might work, but I havent had time to mess with it. Or is what Im asking to do only possible through the string?

OpenStudy (e.mccormick):

Until you are allowed to use exception handlers and streams, you are basically at the mercy of the input and can only use if or select to filter out what you need.

OpenStudy (anonymous):

Numbers as in integers, or longs? Try getting the input as a string and then trying to convert it to a number. If that doesn't work out them cout the error.

OpenStudy (e.mccormick):

@wio the problem there is that if he is not yet allowed to do exceptions, converting a string can crash it. Also, if they have not overed atoi, he is out there too.

OpenStudy (anonymous):

You don't need exceptions. Use a for loop for the characters of the string and make sure they're all `>= '0'` and `<= '9'`, for example.

OpenStudy (e.mccormick):

True, and you can toss an error if the character does not match a number. So if someone says "two" it goes through to do the first character and does not get a number. Each pass through, multiply what you have by 10 to shift over a digit.

OpenStudy (e.mccormick):

Now that I think of it, I wrote my own atoi with a select one time and used the default case to catch the bad results.

OpenStudy (psymon):

Sorry I havent been around. So, this loops stuff Im not sure about. So basically, I came across a problem. I realized when I accidentally put in - 4 for input instead of -4, it gave me feedback that it recognized it as 0 /4 million something. So I figured that i must have a way to be able to not get outputs like that, so I was trying to find a way to prevent that. Soif I am at the mercy of that, the whole "switch (expression)" does me no good?

OpenStudy (e.mccormick):

Actually, the switch and loop or if block and loop helps you prevent that problem.

OpenStudy (psymon):

So how would i go about doing that? This is the only thing I was given on it in class. Sorry about the formatting of it. I guess break means don't perform, I have no idea O.o switch (expression) //must be integral { case value1: statements1; //case value can break ; //appear only once case value2: statements2; break ; . . . case valuen: statementsn; break ; default : statements; //optional break ;

OpenStudy (e.mccormick):

Yah, that is basically it. but many times ... here, found an example: http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm The key part being: ``` switch(grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : case 'C' : cout << "Well done" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; } ```

OpenStudy (e.mccormick):

Now lets put that in a for loop that is as long as the input is. Say you took in your cin to a cstring char input[]. So as you go through you do: `switch (input[i])` and see what that character is. If it is a -, you multiply your number by -1. If it is anything else, you multiply by 10, then add in the number. that way for 0, 0*10... let me walk throug one.

OpenStudy (psymon):

yeah because ive never done anything with strings or even seen it yet.

OpenStudy (e.mccormick):

value = 0 input[] = "342" input[0]='3' So you multiply value = 0 by 10, and it is still 0, then add 3. next time through the loop it is at input[1]=4 multiply value = 3 by 10 to be value = 30. Add 4. now 34 next time through the loop it is at input[2]=2 multiply value = 34 by 10 to be value = 340. Add 2. now 342 end of the number of character put in the string, no more to the loop. This depends on using a cstring, also known as a character array. Used arrays yet?

OpenStudy (psymon):

Nope. I really havent used much at all. Before this it was stuff with aligning and set-precision and such. OtherwiseIm not sure what special stuff there was before. So im notinto much of anything. So.....the select statements. It went to the end and eventually got invalid input. So, what would I need tomake my cases then? Because the only two variables I have are int and denom. But if I just put in - 5, I would think the program would recognize it as an int and denom and still give me something back. So I see how it can work, just no idea how to exclude specific things. Im not sure, maybe you just have to see my code, haha x_x

OpenStudy (e.mccormick):

Well, the problem is that -5 and - 5 are very differnet things to a computer. If you are at the point where you are not needing to test for bad input, it may be simplest just to take in the -5 and not worry about spaces and all that.

OpenStudy (psymon):

I guess im just worried when the professor goes through the program that she would test for something like that, to see what happens if something invalid is put in. I think other than that, I have all the kinks worked out, though.

OpenStudy (e.mccormick):

Until they teach you about exceptions, they generally provide "nice" data.

OpenStudy (psymon):

Alright, Ill just make sure any other kinks I got are worked out then, thanks : )

OpenStudy (e.mccormick):

Kk. Yah, get it running, then if you have time you can experement with doing more. that way you know you have a good copy to hand in and the rest is just icing.

OpenStudy (psymon):

Pretty much. Been trying to save anything I can, I know ll need all the notes and good templatesIcan get xD

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!