How do i make an input that isn't a number

Hi everyone new here. :)

Just started learning C++ the other day just out of sheer curiosity. I have been writing this little program for about a day now (not in total :P) and I have so far accheived what i would like to, but i wish to expand on it a little more.

My program is just a simple pay calculator but I want to expand so that it will calculate the time worked. So for example the console will first off ask me if i would like to Caculate the total time worked or the Total weekly pay.

Then if i type in Total time worked it will call the function WorkTime() If i type in Pay Cal it will call the function PayCal()

I have done some research into this but the only thing i have found is to use #include <string> but i did and i don't know what to do after that lol.

That is where im confused. My code is below if you would like to have a look.

I appreciate the help!


Vhorx

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>

using namespace std;
void PayCal();
void WorkTime();

int main()

{
	string Answer;
        // char Answer

	system("Color 0A");

	cout << "                  ###########################################" << endl;
	cout << "                  ##                                       ##" << endl;
	cout << "                  ##                                       ##" << endl;
	cout << "                  ##                                       ##" << endl;
	cout << "                  ##                                       ##" << endl;
	cout << "                  ##                                       ##" << endl;
	cout << "                  ##                                       ##" << endl;
	cout << "                  ###########################################" << endl;
	cout << endl;
	cout << endl;
	
	cout << "What do you want to do? Caculate your pay or Caculater the ammount of time you have worked?";
	cin >> Answer;

	if (Answer == Yes)
	//if (Answer == 1)
	{
		PayCal();
	}

	else 
	{
		WorkTime();
	}
	

}

void PayCal()  // Calls the Pay Caculator
{
	float AmmountPerHour;
	float TimeWorked;
	float OverTimeHour;
	float TotalPay;
	float TotalOT;
	float TotalOTPay;

	cout << "Enter Time worked: ";
	cin >> TimeWorked;

	if (TimeWorked > 80)
	{
		cout << endl;
		cout << "You work only 80 hours max every two weeks... Fix your shit" << endl;
		cout << endl;
		PayCal();
	}

	
	cout << endl;
	cout << "Enter ammount Per hour: ";
	cin >> AmmountPerHour;
	cout << endl;
	cout << "Enter Total Over time worked: ";
	cin >> OverTimeHour;
	
	TotalPay = AmmountPerHour * TimeWorked;
	TotalOT = OverTimeHour * AmmountPerHour * 1.5;   
	TotalOTPay = TotalPay + TotalOT;

	cout << endl;

	if (OverTimeHour == 0)
	{
		cout << "Your Total Pay this week is: " << TotalPay << endl;
	}

	else
	{
		cout << "Your Total Pay this week is: " << TotalOTPay;
	}



	while(1);

}

void WorkTime()   // Calls the ammount of time you work per day
{
	cout << "test";
	
	while(1);
}
Last edited on
Line 30: if (Answer == "Yes").

It would be more robust if you #include <cctype> and simply test the first letter.

1
2
3
  cin >> Answer;
  if (toupper( Answer[ 0 ] ) == 'Y')
    ...

Hope this helps.

PS. "amount" is spelled with one 'M'.
Thankyou for that Duoas, it has worked. Just one more question, could you possibly explain to me how it worked?

string Answer;

Is that correct, does that actually declare Answer as a variable that can have Alpha charactures in it?

Could you possibly also explain how the "if" statement works, not to sure what toupper means or what the [ 0 ] means? I know Answer is my variable.

Lol at the miss spell *facepalm*. I'll fix that up, thanks :)


Thanks heaps for your help Duoas!


Regard

Vhorx
The std::string class encapsulates an array of char -- which holds the textual information you see on the computer.

When the computer asks you for a number and you type "42" for an answer -- you have given the computer a string -- a list of characters. The >> operator knows how to convert the string "42" into the number 42.

So when you ask whether the user wishes to try again, he will type a string like "yes" or "Yeah!" or "y" or "No way!" or "never again" -- and all you do is check to see if the first character in the string (which index is zero) is a 'Y' or 'y'.

That is a common word to misspell -- I used to do it all the time.

:-)
Topic archived. No new replies allowed.