Hi guys,
me and my friend have both started learning to write C++ and are setting eachother tasks to complete and then compare the programs (thought it would help learning). the task was to create a simple program that could work out a value in a physics equation. This is what ive come up with so far (i know i haven't put any equations in yet, but instead i just wanted to see if my switch worked):
#include <iostream>
usingnamespace std;
int main()
{
system("TITLE Physics Calculator");
char cChar;
do
{
cout << ("This simple tool will allow you to solve any value in the F=G*m1*m2/r^2 equation") << endl;
cout << endl;
cout << ("Press the character corresponding with the value you want to solve:") << endl;
cout << endl;
cout << ("(f) F = Force") << endl;
cout << ("(1) m1 = mass of object 1") << endl;
cout << ("(2) m2 = mass of object 2") << endl;
cout << ("(r) r = distance between two centre of masses") << endl;
cin >> cChar;
switch (cChar)
{
case'f':
cout << "F has been selected";
break;
case'r':
cout << "r has been selected";
break;
case'1':
cout << "1 has been selected";
break;
case'2':
cout << "2 has been selected";
break;
}
system("pause");
return 0;
}
}
any help to why the "Expecting a While" error or any improvements on the program would be greatly appreciated :)
You could try program in an exit key such as 'X' and have your while condition as while( cChar != 'X' );.
There is a better way to do it, but at the moment it alludes me.
Also I see you are using system("pause"), in the current context it's perfectly fine but there are many evils associated with it. You can find about why it's bad and an alternative here:
You'll also want to move your system("pause"); and return 0; out of the the do-while loop because otherwise after the first iteration of the loop the program will close.