PDA

View Full Version : Statments, And Loops (C++)



BullDog_Flash
October 11th, 2005, 02:09 PM
Im not sure how to use the if statment. or same goes with any statment or loop correctly in C++, I seem to be doing it wrong, yet i looked at other source code and it seems i have done it right.

My Way:

#include <iostream>

using namespace std;

int main()
{
for (int x = 0; x < 10; x++) {
cout<< x <<endl;
}

Now see at after the for loop and the cout the Closing bracket for closing the for loop is supposed to line up with the f in for autmoatically it dont and when i do finsh the program it gives me an error. Im not sure whats going on and I need help with this.

www.cprogramming.com way:

#include <iostream>

using namespace std;

int main() // Most important part of the program!
{
int age; // Need a variable...

cout<<"Please input your age: "; // Asks for age
cin>> age; // The input is put in age
cin.ignore(); // Throw away enter
if ( age < 100 ) { // If the age is less than 100
cout<<"You are pretty young!\n"; // Just to show you it works...
}
}

Now there's Does line up perfectly and works great what can I be doing wrong

BullDog_Flash
October 11th, 2005, 02:18 PM
I tried both codes, and neither one had worked

Smee
October 11th, 2005, 06:07 PM
You're missing a closing bracket in the first code example. You open two curly brackets, and only close one.

BullDog_Flash
October 11th, 2005, 08:44 PM
You're missing a closing bracket in the first code example. You open two curly brackets, and only close one.

Yes to show you that the first closing bracket is for the for loop which is supposed to line up with the f but does not. That is only a peiece of the code

Blackspirit
October 12th, 2005, 09:13 AM
Well it works for me, what error are you getting?

It sounds like you might be compiling it wrong if both don't work.

OnionSkin
October 12th, 2005, 10:14 AM
In your first example, try declaring x before you use it in the loop. Also try putting a cin.get()under the cout statement. that will stop the consol window from closing on you.

...cout<< x <<endl;
...cin.get();