I'm new and I dont know why my input wont work?

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  //Test for input

#include <iostream>
using namespace std;

int main ()
{
  int i;
  int a;
  cout << "Hello Brandon, enter a number: ";
  cin >> i;
  cout << "The value you have entered is " << i << ".\n";
  cout << "Double it = " << i*2 << ".\n";
  cout << "Half it = " << i/2 << ".\n";
  cout << "Now enter a two digit number " << "\n";
  cin >> a;
  if ( a = 20 ) cout << "Correct" << "\n";
   if ( a < 20 );
   cout << "Incorrect" << "\n";
  system ("PAUSE");
}


when I run it it prints both output messages (Correct & Incorrect) but I just want it to print correct if the input is equal to 20 and incorrect when it's less than 20 someone please help a fellow new guy :(
You need to use == to check whether numbers are equal, and a semi colon after an if makes it pointless. You should also always use curly braces, even though they are not mandatory, the often make problems with if clearer, especially if you are using a code formatting plugin as they will pick things like this up. Lines 17 to 19 should look like this:
1
2
3
4
5
if( a == 20) {
    cout << "Correct" << "\n";
} else if ( a < 20 ) {
    cout << "Incorrect" << "\n";
}

Although, I would question the logic in this program.
You enter a number
The program prints it doubled
The program prints it halved
The program asks for a two digit number (not specifying that it should be 20)
It then says you are wrong if you put less than 20, says you are right if you put 20, and ignores you if you put more than 20.
Is that what it's meant to do?
Oh I see, thank you for the help :) Yes thats what it's meant to do for now I'm going to work on it and make something for when you type something over 20 :)
Also what do the curly braces actually do? Are they just to set the if statment out clearer? and there isn't really a purpose to the program I'm just playing really :)
Last edited on
The braces in the case of an if tell the if to execute everything inside them if it is true. It's effectively like the program creates a function from what's inside those curly braces which the if runs if it is true. The case of an if without curly braces is actually an if with implicit curly braces that surround the command after the if and nothing else. In the following scenario:
1
2
3
if(a==20)
func1();
func2();

func2 will always be called and func1 will only be called if a is equivalent to 20 because it is a way of writing this:
1
2
3
4
if(a==20){
func1();
}
func2();

Using curly braces, we can control how much code the if is responsible for so we could do this
1
2
3
4
if(a==20){
func1();
func2();
}

where either both functions are called (because a == 20) or neither are called.
Basically they tell the if which bit of the program should only be run if it evaluates to true.
if ( a = 20 ){
cout << "Correct" << "\n";
} else if ( a < 20 )
cout << "Incorrect" << "\n";
else if ( a > 20 ) {
cout << "Above 20" << "\n":
} system ("PAUSE");
}
Okay I'm understanding it more now :) How would I add the third output if the input is higher than 20? I keep getting errors when running but I'm not sure what to do :S The compiler says "expected ';' before ':' token :S
Never mind I realized I had a colon and not a semi-colon :S this may take a while
Topic archived. No new replies allowed.