Logic Error (Simple IF Statement)
I have the following block of code...
Code:
int counter2 = 0;
while(counter2 < 1) {
printf("What time of the day were you travelling at? Was it at the peak
scanf("%d", &timeofday);
if(timeofday == 1 || timeofday ==2) {
counter2 = counter2 + 1;
}
else
printf("ERROR; incorrect input.");
}
I wanted to keep prompting the user for this question until the integer "1" is entered. I don't think the IF statement is ever true regardless if I were to enter a 1, or not. Simply because I am comparing the input from the user with an integer, which i believe is the problem. But.. how would i be able to fix this?
Re: Logic Error (Simple IF Statement)
1. parse the input to get an integer
2. compare string to string, something like
Code:
if(timeofday == "1" || timeofday == "2") {
Re: Logic Error (Simple IF Statement)
Quote:
Originally Posted by
boudino
1. parse the input to get an integer
2. compare string to string, something like
Code:
if(timeofday == "1" || timeofday == "2") {
When I tried to use the quotations and compare string to string, the compiler said that i was trying to compare between a pointer and an integer.
Re: Logic Error (Simple IF Statement)
Quote:
2. compare string to string, something like
The code sample appears to be C++ - try the c++ forums not the C# forums.
Darwen.
Re: Logic Error (Simple IF Statement)
Quote:
Originally Posted by
darwen
The code sample appears to be C++ - try the c++ forums not the C# forums.
Darwen.
This is what I have tried to do in my attempt to parse the user input, still generate error.
Code:
int timeParse = 0;
do{
while(counter2 < 1) {
printf("What time of the day were you travelling at? Was it at the peak
scanf("%d", &timeofday);
int timeParse = int.Parse(timeofday);
printf("made it here");
counter2 = counter2 + 1;
if(timeParse == 1 || timeParse == 2){
counter2 = counter2 + 1;
}
else{
printf("ERROR; Please enter a valid input.");
}
}
ERROR: assignment1.c:16: error: expected expression before 'int'
Re: Logic Error (Simple IF Statement)
scanf and printf are both C/C++ methods. They don't exist in C#.
Are you trying to write a C# application or a C/C++ application ?
You are aware that they are completely different languages ?
Darwen.
Re: Logic Error (Simple IF Statement)
I think the intention is clear, rewrite it to the language you are using. I think that it could be something like
Code:
if(timeofday* == "1" || timeofday* == "2")