-
Beginners help
Ive been practicing some C++ lately and I cant seem to see why this dont work....I believe theres maybe some ; missing in the if statement ?
As u can tell from the code im a total newby to this...so any help is appreciated =)
Code:
int Main()
{
int A,B;
cout << "Enter two numbers \n";
cin >> A;
cin >> B;
if A > B
cout << "Your first number was bigger \n"
else
cout << "\n Your second number was bigger \n"
return 0;
}
Last edited by process; 06-20-2004 at 04:24 PM.
-
Re: Beginners help
Originally posted by process
Ive been practicing some C++ lately and I cant seem to see why this dont work....I believe theres maybe some ; missing in the if statement ?
As u can tell from the code im a total newby to this...so any help is appreciated =)
Code:
int Main()
{
int A,B;
cout << "Enter two numbers \n";
cin >> A;
cin >> B;
if A > B
cout << "Your first number was bigger \n"
else
cout << "\n Your second number was bigger \n"
return 0;
}
make sure you put the ; and the end of your expessions. and also the if should be like this
Code:
int main()
{
int A, B;
cout << "Enter two numbers: ";
cin >> A >> B;
if( A > B )
cout << "Your first number is bigger." << endl;
else
cout << "Your second number is bigger." << endl;
return 0;
}
hope this helps
Last edited by ben; 06-20-2004 at 06:27 PM.
__________________
Thks! Ben
-
make sure you understand that the if statement syntax for c++ is:
if (conditional) // either true or false only
{ // execute statements if conditional was true
statements ; // always end a statement with a semicolon
} // end of block for if statements
the virtuous lead with their minds.
-
so also the if statements are contained within a block like
Code:
if( A > B )
{
cout << "Your first number is bigger." << endl;
else
cout << "Your second number is bigger." << endl;
}
return 0;
}
:confused:
-
Originally posted by process
so also the if statements are contained within a block like
//..... I assume some code is here that starts a function....
if( A > B )
{
cout << "Your first number is bigger." << endl;
} // Closing the true part of the if....
else
{ // starting new block for else (false part of if)....
cout << "Your second number is bigger." << endl;
}
return 0;
}
Because the blocks for this if....else statement are one line, you could also have done:
if( A > B )
cout << "Your first number is bigger." << endl;
else
cout << "Your second number is bigger." << endl;
return 0;
}
Note that the above doesn't show indenting
-------------------------------------------------
Bradley L. Jones
Author, and more.
QuinStreet Enterprise Network / Developer.com Network
(Developer.com, CodeGuru.com, DevX, jGuru, and more!)
-------------------------------------------------
-
thx for all ur help chaps...seems more clear now :cool:
-
What is the "endl" after each statement? I thought the semi-colon was enough to end a statment. But you guys put << endl;??? what's this for? :confused:
-
signals a new line
endl
endline
-
What's wrong with using \n for signalling a new line? Is "endl" just an alternative?
-
Originally posted by Blade195
What is the "endl" after each statement? I thought the semi-colon was enough to end a statment. But you guys put << endl;??? what's this for? :confused:
If you want to have the user input data right after the output as in cout << "please enter your name"; <- leaves cursor on the current line;
Doing it this way cout << "Please enter your name " << endl;
moves the cursor to a new line below "Please enter your name".
I thought I read somewhere that endl clears the buffer where in "\n" does not. Feel free to correct me if im wrong.
__________________
Thks! Ben
-
endl does two things: i flushes the stream, which means forcing output to the screen without delay, and it moves it to the next line.
Danny Kalev
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|