Jump to content
  • Advertisement
Sign in to follow this  
Haku

C++ noob basic question

This topic is 5142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why wont this code compile?
[source language=c++]
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
int main()
{
int myage=15;
int askage;
    
cout<<"hello, how old are you?";  
cin>>askage
if ( askage>myage);
{
    cout<<"you are older than me";
}
if (askage<myage)
{
    cout<<"you're younger than me";      
          
    cin.get();
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
cin>>askage

add a ; to end the statement. This is a compile error.

cin>>askage;

if ( askage>myage);

remove the ; to allow the next set of statements to run

if ( askage>myage)
{
// Stuff
}

With the ; placed at the end of the parenthesis, your if statement will end there and never run the stuff between the braces. This will compile, but it wont run right.

Share this post


Link to post
Share on other sites
if ( askage>myage);
{
cout<<"you are older than me";
}


Remove the semicolon in the first line, so it becomes

if ( askage>myage)
{
cout<<"you are older than me";
}


This should work. ;)

Share this post


Link to post
Share on other sites
The code you posted is missing a closing brace. Add a '}' in between 'cout << "you're younger than me";' and 'cin.get()'.

If you get additional errors, it'll help if you post specifically what errors you are getting.

Share this post


Link to post
Share on other sites
You made a few small mistakes in your code. You left out a closing bracket at the end of your main function, you put a semicolon after your first if statement and you left one out on your cin>>askage line.

Heres the touched up code.


#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
int main()
{
int myage=15;
int askage;

cout<<"hello, how old are you?";
cin>>askage;
if ( askage>myage)
{
cout<<"you are older than me";
}
if (askage<myage)
{
cout<<"you're younger than me";
}

cin.get();
return 0;
}



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your return 0 is in your if statement, and therefore there is no return in all paths.

Place the return outside the IF statement and it'll be fine (may not what do what you want it to do, but thats a whole different question).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it's not whether you win it's how you play...no, it's all about the winning....

Share this post


Link to post
Share on other sites
Anyway, each one pointed out an error, and that's quite cool...
This is what a forum is all about, right?

Share this post


Link to post
Share on other sites
right, now i want the input line of code to be under the question, where do i put the \b in?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!