Write your question here.

Hi, this is a really basic code that asks users to input two numbers (whole and then later fractional) and preforms some basic calculations. So far it is successful, but what I cannot figure out is how I should go about sending the user an invalid message "please enter only whole numbers" if they enter something other than a whole number in the first part ( a letter, a fraction, ect.) and also I want to create a code so that if the user enters 'END' the program will terminate. I was thinking maybe using a do, while loop for the first part but am looking for suggestions as I am not 100% sure how to do that.

thanks for your time,

lizzy

#include "stdafx.h"

#include <iostream>

#include <math.h>

#include <stdlib.h>

#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int wholenumber1 = 0;

int wholenumber2;

int wholesum, wholedifference, wholedivision, wholemultiplication, wholemodulus;

//wholenumbers:

std::cout << "please enter your first whole number and hit enter:" << endl;

cin >> wholenumber1;

std::cout << "you entered " << wholenumber1 << endl;

cout << "please enter your second whole number and hit enter:" << endl;

cin >> wholenumber2;

std::cout << "you entered " << wholenumber2 << endl;

cout << "your two numbers are:" << wholenumber1 << "and" << wholenumber2 << endl;

wholesum = wholenumber1 + wholenumber2;

wholedifference = wholenumber1 - wholenumber2;

wholedivision = wholenumber1 / wholenumber2;

wholemodulus = wholenumber1%wholenumber2;

wholemultiplication = wholenumber1 * wholenumber2;

cout << "the sum of the two is = " << wholesum << endl;

cout << "the difference between the first number and the second number is =" << wholedifference << endl;

cout << "the first number divided by the second is =" << wholedivision << endl;

cout << "the first number times the second is =" << wholemultiplication << endl;

cout << "the first number modulus the second is =" << wholemodulus << endl;

cout << endl;

// fractional numbers:

float fractionalnumber1;

float fractionalnumber2;

float fractionalsum, fractionalmultiplication, fractionaldivision, fractionaldifference;

cout << "please enter your first fractional number:" << endl;

cin >> fractionalnumber1;

std::cout << "you entered " << fractionalnumber1 << endl;

cout << "please enter your second fractional number:" << endl;

cin >> fractionalnumber2;

std::cout << "you entered " << fractionalnumber2 << endl;

cout << "your two chosen fractional numbers are:" << fractionalnumber1 << "and"<< fractionalnumber2 << endl;

fractionalsum = fractionalnumber1 + fractionalnumber2;

fractionaldifference = fractionalnumber1 - fractionalnumber2;

fractionalmultiplication = fractionalnumber1 * fractionalnumber2;

fractionaldivision = fractionalnumber1 / fractionalnumber2;

cout << "the sum of the two is = " << fractionalsum << endl;

cout << "the difference between the two is =" << fractionaldifference << endl;

cout << "the first number times the second is =" << fractionalmultiplication << endl;

cout << "the first number divided by the second is =" << fractionaldivision << endl;

return 0;

}

Hi, this is a really basic code that asks users to input two numbers (whole and then later fractional) and preforms some basic calculations. So far it is successful, but what I cannot figure out is how I should go about sending the user an invalid message "please enter only whole numbers" if they enter something other than a whole number in the first part ( a letter, a fraction, ect.) and also I want to create a code so that if the user enters 'END' the program will terminate. I was thinking maybe using a do, while loop for the first part but am looking for suggestions as I am not 100% sure how to do that.

thanks for your time,

lizzy

#include "stdafx.h"

#include <iostream>

#include <math.h>

#include <stdlib.h>

#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int wholenumber1 = 0;

int wholenumber2;

int wholesum, wholedifference, wholedivision, wholemultiplication, wholemodulus;

//wholenumbers:

std::cout << "please enter your first whole number and hit enter:" << endl;

cin >> wholenumber1;

std::cout << "you entered " << wholenumber1 << endl;

cout << "please enter your second whole number and hit enter:" << endl;

cin >> wholenumber2;

std::cout << "you entered " << wholenumber2 << endl;

cout << "your two numbers are:" << wholenumber1 << "and" << wholenumber2 << endl;

wholesum = wholenumber1 + wholenumber2;

wholedifference = wholenumber1 - wholenumber2;

wholedivision = wholenumber1 / wholenumber2;

wholemodulus = wholenumber1%wholenumber2;

wholemultiplication = wholenumber1 * wholenumber2;

cout << "the sum of the two is = " << wholesum << endl;

cout << "the difference between the first number and the second number is =" << wholedifference << endl;

cout << "the first number divided by the second is =" << wholedivision << endl;

cout << "the first number times the second is =" << wholemultiplication << endl;

cout << "the first number modulus the second is =" << wholemodulus << endl;

cout << endl;

// fractional numbers:

float fractionalnumber1;

float fractionalnumber2;

float fractionalsum, fractionalmultiplication, fractionaldivision, fractionaldifference;

cout << "please enter your first fractional number:" << endl;

cin >> fractionalnumber1;

std::cout << "you entered " << fractionalnumber1 << endl;

cout << "please enter your second fractional number:" << endl;

cin >> fractionalnumber2;

std::cout << "you entered " << fractionalnumber2 << endl;

cout << "your two chosen fractional numbers are:" << fractionalnumber1 << "and"<< fractionalnumber2 << endl;

fractionalsum = fractionalnumber1 + fractionalnumber2;

fractionaldifference = fractionalnumber1 - fractionalnumber2;

fractionalmultiplication = fractionalnumber1 * fractionalnumber2;

fractionaldivision = fractionalnumber1 / fractionalnumber2;

cout << "the sum of the two is = " << fractionalsum << endl;

cout << "the difference between the two is =" << fractionaldifference << endl;

cout << "the first number times the second is =" << fractionalmultiplication << endl;

cout << "the first number divided by the second is =" << fractionaldivision << endl;

return 0;

}

in order to restrict the user to add whole numbers only , u can use if-else statements like

cout<<"enter whole numbers"<<endl;

cin>>x;

if(x>=0) // as whole numbers are greater than equal to zero !

.

.

.

your calculations

.

.

.

else

cout<<"invalid Input"<<endl;

cout<<"enter whole numbers"<<endl;

cin>>x;

if(x>=0) // as whole numbers are greater than equal to zero !

.

.

.

your calculations

.

.

.

else

cout<<"invalid Input"<<endl;

I gave that a try and the program, when run, does not recognize it and when fractions(decimals) are entered it continues to freak out. Could you take a look at the code and tell me if I entered it correctly?

int wholenumber1;

int wholenumber2;

int wholesum, wholedifference, wholedivision, wholemultiplication, wholemodulus;

//wholenumbers:

std::cout << "please enter your first whole number and hit enter:" << endl;

cin >> wholenumber1;

if (wholenumber1 >= 0)

{

std::cout << "you entered " << wholenumber1 << endl;

}

else

{

cout << "Invalid Input, please enter a whole number and hit enter" << endl;

cin >> wholenumber1;

}

int wholenumber1;

int wholenumber2;

int wholesum, wholedifference, wholedivision, wholemultiplication, wholemodulus;

//wholenumbers:

std::cout << "please enter your first whole number and hit enter:" << endl;

cin >> wholenumber1;

if (wholenumber1 >= 0)

{

std::cout << "you entered " << wholenumber1 << endl;

}

else

{

cout << "Invalid Input, please enter a whole number and hit enter" << endl;

cin >> wholenumber1;

}

Topic archived. No new replies allowed.