Wrong answer. wrong answer. wrong answer. wrong answer........

in this program i try to make it similar as TEST if i write the right answer it will say RIGHT if my answer is wrong tell me that's wrong.
The program's written in C, the problem is that i get "wrong answer try again "
but it should work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <stdio.h >


int main ()
{
	char string1[20];
	char  a='a', b='b',  c='c', d='d',  e='e';

	int rigi;
	int sum;

	printf (" type abcde?\n");

	scanf_s( "%19s", string1);
	sum=0;
	
for (rigi = 0; string1[rigi] !='\0'; rigi++)
    {
	
	if (string1[rigi]==a ) {sum= sum +1;} else
	if (string1[rigi]==b ) {sum= sum +2;} else
	if (string1[rigi]==c ) {sum= sum +3;} else
	if (string1[rigi]==d ) {sum= sum +4;} else
	if (string1[rigi]==e ) {sum= sum +5;} else
	
	{sum=0;}
}
	
		
if (sum==15)	{ printf("right! \n");}
		else
		{printf ("wrong answer try again \n");
	   }
	return 0;
}
Last edited on
What is happening on line 26?
else > like if i type something not "abcde" for ex "z" somewhere the sum will be 0 and it should mean that there is a mistake
But what is the value of sum at line 29? How can it ever equal 21 - what is the most it could get to ?


I am yet a very novice programmer (let alone C++ programmer) but would I be wrong to say that line no. 26 would be a stand-alone statement and is not attatched to the if... else... statement? Perhaps this is always setting 'rigi' to '0', regardless of preceeding assignments?
I am yet a very novice programmer (let alone C++ programmer) but would I be wrong to say that line no. 26 would be a stand-alone statement and is not attatched to the if... else... statement?

Yes, you would be wrong to say that. C++ ignores the whitespace between the else at the end of line 24 and the { on line 26.
@OP: Please indent your code properly.
Also, provide a testcase.
Oh, thought I could be.
Sorry for any misdirection I my have encouraged :/

cout << "Running in shame... :(";
Topic archived. No new replies allowed.