2 Replies - 959 Views - Last Post: 29 January 2016 - 08:31 AM

#1 marinabl  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 01-November 08

Help using the "if" statement

Posted 28 January 2016 - 09:53 PM

Good day all,
yes like many i'm new to C++ as you can see from my below code, my issue with this code is that I cannot get it to stop if the value is false; how can I terminate the program if they put in a number out of the asked for range? any suggestions would be greatly appreciated

Purpose of this is to prompt the user to enter a number which is an integer in the range of 0 - 100 
// using if statement the program output will output an error message if the score is too low or too high


#include <iostream> // allows the program to perform input and output

using std::cout; // program uses cout
using std::cin; // program uses cout
using std:: endl; 

int main()
{
	//variable declarations
	int number1; // the integer to evaluate
		
	cout << "Enter an Integer between 0 and 100: ";  // prompts the user to enter in the first integer of data
	cin >> number1;  // read the integer form the user

		if ( number1 <= 0 ); 
		cout << number1 << " is less than the value of 0 try again\n "; // ISSUE IS HERE
	
		if( number1 >= 100 );
		cout << number1 << " is more than the value of 100 try again \n "; // 

	cout << "Thank you for entering within the correct range your number was  " << number1 << endl;  // display the sum and end line
	
	 } // end function main

This post has been edited by Atli: 28 January 2016 - 11:23 PM
Reason for edit:: Fixed [code] tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Help using the "if" statement

#2 naaissus  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 10
  • Joined: 27-January 16

Re: Help using the "if" statement

Posted 29 January 2016 - 12:38 AM

Quote

if ( number1 <= 0 );
cout << number1 << " is less than the value of 0 try again\n ";
if( number1 >= 100 );
cout << number1 << " is more than the value of 100 try again \n ";


Those semicolons are just like period in the middle of the sentence.
"If number is >= 100. Say something." These two sentences (statements) aren't connected, so I will say something no matter given condition.
So, as soon you put ';' you are done with that statement. That's why none of your cout statements are inside if branches and they both execute no matter input.

Your code should look like this:
if ( number1 <= 0 )
    cout << number1 << " is less than the value of 0 try again\n ";
if( number1 >= 100 )
    cout << number1 << " is more than the value of 100 try again\n";

Was This Post Helpful? 0
  • +
  • -

#3 marinabl  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 01-November 08

Re: Help using the "if" statement

Posted 29 January 2016 - 08:31 AM

View Postnaaissus, on 29 January 2016 - 12:38 AM, said:

Quote

if ( number1 <= 0 );
cout << number1 << " is less than the value of 0 try again\n ";
if( number1 >= 100 );
cout << number1 << " is more than the value of 100 try again \n ";


Those semicolons are just like period in the middle of the sentence.
"If number is >= 100. Say something." These two sentences (statements) aren't connected, so I will say something no matter given condition.
So, as soon you put ';' you are done with that statement. That's why none of your cout statements are inside if branches and they both execute no matter input.

Your code should look like this:
if ( number1 <= 0 )
    cout << number1 << " is less than the value of 0 try again\n ";
if( number1 >= 100 )
    cout << number1 << " is more than the value of 100 try again\n";








Thank you Naaissus,
Looking at it in that perspective makes total sense, I really appreciate your feedback on this, im learning independently on this (on my own), I hear once you learn C++ the other languages will be really easy. Looking forward to tinkering in this, thank you again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1