-
C++ Quiz Problem
Hi
Im new to this forum, I just signed up ;)
I have a problem with my code. I am trying to make a quiz, it was working fine yesterday. I tried to debug it today and i got 29 errors! :(
I dont understand how that happened, I have looked at the output and I just dont see whats wrong with it. Any help would be greatly appreciated, and yes I a noob :)
Code is below:
Code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
int main();
}
char a;
printf("GENERAL QUIZ\n");
printf("\n");
printf("Who is the chairman of Microsoft Corp?\n");
printf("1 Bill Gates\n");
printf("2 Steve Jobs\n");
printf("3 Jake Taylor\n");
a=getchar();
if (a=='1') printf("Correct\n"); else printf("Wrong\n");
while (a!='z');
{
a=getchar();
{
EDIT: You'll have to explain things carefully as I can get easily confused :D
-
There is an excess semicolon after main(), and another after while(). Your code block brackets don't match. You always begin a block with {, and end with }. If you start, you must end it, therefore there will always be an even number of brackets.
1st bracket: backwards
2nd: ok
3rd: backwards
4th: awol
Also, you are missing a return 0; statement at the end of main().
-
You should learn how to indent your code properly as well for readability purposes, especially when you're debugging.
-
Ok thanks. I'll try what you (rossipoo) suggested later. I'll let you know how I get on. Thanks again :)
-
For future reference, it helps if you post the errors that you receive. :D
-
Ok, Im on one error now. I still dont understand it though.
: fatal error C1075: end of file found before the left brace '{' at 'c:\users\------ ----------documents\visual studio 2008\projects\test1.1\test1.1\test1.1.cpp(6)' was matched
-
-
Sorry. I have to get used to this :)
Code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
int main()
{
char a;
printf("GENERAL QUIZ\n");
printf("\n");
printf("Who is the chairman of Microsoft Corp?\n");
printf("1 Bill Gates\n");
printf("2 Steve Jobs\n");
printf("3 Jake Taylor\n");
a=getchar();
if (a=='1') printf("Correct\n"); else printf("Wrong\n");
while (a!='z')
{
a=getchar();
}
-
You get the error because your braces don't match, like pointed out earlier.
Code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
int main()
{
char a;
printf("GENERAL QUIZ\n");
printf("\n");
printf("Who is the chairman of Microsoft Corp?\n");
printf("1 Bill Gates\n");
printf("2 Steve Jobs\n");
printf("3 Jake Taylor\n");
a=getchar();
if (a=='1') printf("Correct\n"); else printf("Wrong\n");
while (a!='z')
{
a=getchar();
}
}
Also, look into indentation. :D
-
WOW. It works and I feel so stupid, haha :)
Thanks guys, it really means alot. I hope I'll stay here and learn alot from you guys.
Thanks again
-
No problemo! ;)
By the way, do you realise that your print functions are in C, not C++?
-
Actaully no I didn't. :D
My teacher showed me then, he mainly used C and not C++.
How do I change these to C++ Print Functions, and whats the difference between the C version and the C++ version?
Thanks :)
-
Well I'm not a C programmer, so I can't tell you the difference between the two, but I can tell you how to translate between the two.
To print text to the console, use std::cout followed by the output operator <<
To acquire user input and store it into a variable, use std::cin followed by the input operator >>
I'll demonstrate:
Code:
#include <iostream>
int main()
{
char a;
std::cout << "GENERAL QUIZ\n";
std::cout << "\n";
std::cout << "Who is the chairman of Microsoft Corp?\n";
std::cout << "1 Bill Gates\n";
std::cout << "2 Steve Jobs\n";
std::cout << "3 Jake Taylor\n";
std::cin >> a;
if (a == '1')
{
std::cout << "Correct\n";
}
else
{
std::cout << "Wrong\n";
}
while (a != 'z')
{
std::cin >> a;
}
}
As you can see, I've removed outdated header files that aren't needed for the program, and replaced your C functions with C++ functions. The braces highlighted are my programming style although, feel free to use your own. :P
-
Ah i see the difference.
Thank you :)