-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
executable file
·141 lines (128 loc) · 4.48 KB
/
input.cpp
File metadata and controls
executable file
·141 lines (128 loc) · 4.48 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/***************************************************************************************
** Program Filename: input.cpp
** Author: Edwin Grove
** Date: 28/1/2016
** Description: Checks user input of integers and doubles for validity
** Input: string s
** Output: returns integer or double depending on function
****************************************************************************************/
#include "input.hpp"
#include <iostream>
#include <string>
#include <stdlib.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
// default constructor- initializes a couple of private variables
input::input()
{
decCount = 0;
check = false;
negNum = 0;
}
/****************************************************************************************
** Function: unsignedInt()
** Description: Checks to make sure that user inputs a positive integer
** Parameters: none
** Pre-conditions: none
** Post-conditions: returns an integer value
*****************************************************************************************/
int input::unsignedInt()
{
check = false; //loops until user enters a correct value
while (check != true)
{
cin >> s; //user input as string
for (int i = 0; i < s.size(); i++) //loops through user input to check each character
{
if (s.at(i) > 48 && s.at(i) < 58) //checking if input is an integer by ascii value
{
check = true;
}
else
{
cout << "Please enter a number according to the menu choices" << endl;
check = false; //change check so that function loops again to ask for new input
break;
}
}
if (check == true)
return atoi(s.c_str()); //if valid input returns user value
}
}
/****************************************************************************************
** Function: signedInt()
** Description: Checks to make sure that user inputs an integer (positive or negative)
** Parameters: none
** Pre-conditions: none
** Post-conditions: returns an integer value
*****************************************************************************************/
int input::signedInt()
{
check = false; //loops until user enters a correct value
while (check != true)
{
cin >> s; //user input as string
if (s.at(0) == '-')
{
negNum++;
}
for (int i = 0; i < s.size(); i++) //loops through user input to check each character
{
if ((s.at(i) > 47 && s.at(i) < 58) || negNum > 0) //checking if input is an integer by ascii value
{
check = true;
negNum = 0;
}
else
{
cout << "Please enter an integer value: ";
check = false; //change check so that function loops again to ask for new input
break;
}
}
if (check == true)
return atoi(s.c_str()); //if valid input returns user value
}
}
/******************************************************************************************
** Function: checkDouble()
** Description: Checks user input to see if it is a valid double value
** Parameters: none
** Pre-conditions: none
** Post-conditions: returns user input in form of double
*******************************************************************************************/
double input::checkDouble()
{
check = false;
while (check != true) //loop function until valid user input
{
decCount = 0; //reset count of decimals after each loops
cin >> s;
for (int i = 0; i < s.size(); i++) //check each character in user input
{
if (s.at(i) > 47 && s.at(i) < 58) //check that user input is an integer using ascii values
{
check = true;
}
else if (s.at(i) == '.') //check if decimal value is part of input
{
decCount++;
if (decCount > 1) //if more than one decimal than input is invalid
{
check = false;
cout << "Please enter a valid decimal number: ";
break;
}
}
else //if input is another other than integer or decimal asks for new input
{
check = false;
cout << "Please enter a positive value: ";
break;
}
}
}
return atof(s.c_str()); //returns double value
} //thank god I'm done writing these comments. I should really do this while I'm coding the functions although i did get to listen to David Bowies new album some more (so good)