Invalid code?


Hi, this is a really basic code that asks users to input two numbers (whole and then later fractional) and performs some basic calculations. The issue lies in the if else statement that tells the user that their entry is something other than a whole number. How do I set it up/ what loop should I use so that the user can try again? if I try to enter something that is invalid the message comes up and the rest of the code just freaks out and terminates. What am I doing wrong?
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	int wholenumber1;
	int wholenumber2;
	int wholesum, wholedifference, wholedivision, wholemultiplication; // wholemodulus;

	//wholenumbers:
	cout << "please enter your first whole number and hit enter:" << endl;
	cin >> wholenumber1;
	if (wholenumber1 >= 0)
		cout << "you entered " << wholenumber1 << endl;
	else
		cout << "Invalid Input, please enter a whole number and hit enter" << endl;
	cin >> wholenumber1;
	
	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;
}


  


Topic archived. No new replies allowed.