Problem with SIMPLE code(am noob)

+1 kevin swardh · January 13, 2016
#include <iostream>

using namespace std;


void second();

int main()

{
    second();

    int a;
    int b;
    char userName [20];

        cout << " Hello! my name is kevin\n What is you first name? \n";
         cin >> userName;
        cout << "Hello there " << userName << " what an handsome name!\n So how old are you? \n";
         cin >> a;

        cout << "oh so you are  \n" << a << endl;
        cout << "How old do you think i am? \n";
         cin >> b;

    if[21]{
        cout << "Thats Correct ! You guessed my age rigth! 21 \n";
        cout << "So we are " << (a + b) << " years old together\n";
    }else{
        cout << "Sadly you did not guess my age rigth! please start over. \n";
        cout << "But i calculated that we would be " << (a + b) << "if i was" << b << "years old";
    }

    return 0;
}

    void second(){
    cout << "Lets get to know eachother\n" << endl;
}

Does anyone understand what am trying to do? its simple i know but am new to this, and i cant seem to figure out the if / else statment, and sometime it calculates wrong please help anyone <3

Post a Reply

Replies

Oldest  Newest  Rating
0 Akhilesh Chobey · January 17, 2016
Hello Kevin.
You haven't written the if conditional correctly.
It should go like this:
if(a==21){
cout << "Thats Correct ! You guessed my age rigth! 21 \n";
cout << "So we are " << (a + b) << " years old together\n";
}

You want to compare the user input 'a'. I.e Age.
Also I dint understand your 'second' function.
What is it for?
According to me remove that void second(); at the top and at the bottom after return statement.
Hope that helps :)
0 phapha pha · January 13, 2016

on if[21]{



you make 2 mistake
you should () instead of []

and, inside () , there should be a comparasion
    for your case, it should be if(b==21)

you should watch bucky video about if else statement

this is the summary

if(true){

IF IT'S TRUE
THIS LINE IS WHAT IS GONNA HAPPEN

}else{

IF IT'S FALSE
THIS LANE IS WHAT IS GONA HAPPEN
}
  • 1

C++

107,190 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator