Many Error Messages in my C++ Program
I am new to programming and don't know what is wrong. I've tried to fix it for about a day now.
Quote:
#include <iostream>
#include <string>
int main ()
{
using std::cout;
using std::cin;
using std::string;
string enter = "";
int gold;
int number;
int america;
int newgold;
int twogold, city, threegold;
cout << "Welcome to the coin game. "
<< "The objective is to get 1000 coins by the end.\n"
<< "(Press 1 and hit enter to continue.)\n";
cin >> enter;
cout << "\nFirst, solve 432 * 23, "
<< "in order to get your first 50 coins "
<< "(no calculators!).\n";
cin >> number;
if (number != 9936)
cout << "Sorry, you do not get 50 coins.";
else {
cout << "Congratulations, you now have 50 coins!";
gold = 0 + 50;
}
cout << " The next test is to figure out how old America is as of 2011.\n"
<< " (worth 75 coins.)\n";
cin >> america;
if ( america != 235 ) {
cout << "Sorry, you do not get 75 coins.\n"
<< "You have "
<< gold
<< " coins.";
}
else {
cout << "Congratulations, you now have "
<< (gold + 75)
<< " coins!\n";
newgold = gold + 75;
}
cout << "Now for your next question.\n"
<< "What is N.Y. City's approximate population?";
cin >> city;
if (city != 8000000)
twogold = newgold + 0;
cout << "Sorry, you do not get 75 coins.\n"
<< "You still have "
<< twogold
<< " coins.";
else { // Error: Expected '}' before 'else'.
cout << "Congratulations, you now have "
<< twogold = newgold + 75;
<< " coins.";
}
using std::cout;
using std::cin;
cout << "This next question is worth 200 coins!\n"
// Error: Expected constructor, destructor, or type conversion before '<<' token.
<< "How old would Martin Luther King Jr. "
<< "be as of January 1, 2011?";
cin >> king; // Error: Expected constructor, destructor, or type conversion before '>>' token.
if (king != 0 ) // Error: Expected unqualified-id before 'if'.
threegold = twogold + 0
cout << "Yikes! " // Error: Expected constructor, destructor, or type conversion before '<<' token.
<< "You do not get 200 coins.\n"
<< "You still have "
<< threegold
<< " coins."
else {
threegold = twogold + 200
cout << "Congratulations, you get 200 coins!"
<< "You now have "
<< threegold
<< " coins."
}
return 0; // Error: Expected unqualified-id before return.
} // Error: Expected declaration before '}' token.
Re: Many Error Messages in my C++ Program
If you have more than one statement after the if condition you need to surround it by braces.
Code:
if (condition) {
// Statement 1...
// Statement 2...
}
At least your first else doesn't match because of that apparently.
Re: Many Error Messages in my C++ Program
Quote:
Originally Posted by
ltcmelo
If you have more than one statement after the
if condition you need to surround it by braces.
Code:
if (condition) {
// Statement 1...
// Statement 2...
}
At least your first
else doesn't match because of that apparently.
Thanks. That seemed to fix it. But do you know how to make my code into an application? Or is that too complicated for you to tell me?
Re: Many Error Messages in my C++ Program
But this is an application, it's a Win32 console application. Right? Do you mean you want an application with a GUI? In this case you'd have to describe how it should look and what to do. But you probably want to create an MFC dialog based application.
Re: Many Error Messages in my C++ Program
Quote:
Originally Posted by
cilu
Describe how it should look and what to do. But you probably want to create an MFC dialog based application.
Yes, I do want a GUI. With a basic white background and the words to be in a corner as a chat type thing.
Re: Many Error Messages in my C++ Program
Quote:
Originally Posted by
prattcmp
Yes, I do want a GUI. With a basic white background and the words to be in a corner as a chat type thing.
In that case you need to create a new project.
Re: Many Error Messages in my C++ Program
Quote:
Originally Posted by
Skizmo
In that case you need to create a new project.
Umm. That doesn't really help much. Explain a bit further please?
Re: Many Error Messages in my C++ Program
your (linker) subsystem is console. If you want a gui using windows messages event loop, you need to change the subsystem, or create a new studio project (win32 project) with better defaults. This will give you a WinMain or _tWinMain (or similar), instead of your current main().
Re: Many Error Messages in my C++ Program
Quote:
Originally Posted by
prattcmp
Umm. That doesn't really help much. Explain a bit further please?
Create a new project with the Visual Studio and you will see that you have the option (amongst other project types) to create a console program (what you have now) and you can create a GUI program (MFC).