Reputation: 159
#include <iostream>
using namespace std;
int main()
{
double propValue, //Property Value
name, //Full Name
assessment, //Assessment
srAssessment, //Sr Assessment
taxRate, //Tax rate
annualPropTax; //Annual Property tax
const double EXEMPT = 5000, //shows the total after exemption
QUARTER = 4, //represents the amount of quarters in year
TAXPERHUNDRED = 0.01, //represents tax rate for every $100
SIXTYPERCENT = 0.6; //Represents the tax based on 60% of original value
//Gets name from user
cout << "Please enter your full name: ";
cin >> name;
//gets property value from user
cout << "Enter the actual value of the property: ";
cin >> propValue;
//Gets tax rate
cout << "Enter the tax rate for each $100 of assessed value: ";
cin >> taxRate;
}
I assume there is something that i am doing wrong here, but i cant figure it out. Im new to C++ and to programing as a whole. When i run this code it doesnt let me input anything after the "name" variable it just displays all of my output
Upvotes: 2
Views: 92
Reputation: 70401
Input from std::cin
will fail if the input cannot be converted to the type of the variable.
In your code, you ask for a name, but the variable name
is of type double
-- if you enter "acm818", the 'a'
at the beginning will fail to match. Nothing is stored in name
(which therefore remains uninitialized), the input is not consumed, the program continues.
(You could check for this condition via if ( cin )
-- that will fail if the input operation failed.)
Then you ask for a value, again reading into a double
variable (propValue
). There is still the "acm818"
waiting in the input queue, so the program does not pause to ask for new input, but tries again to parse that string into a double
, and again the matching fails.
Same for taxRate
.
And even if you had name
declared to the proper type, cin >> name
would only read up to the first whitespace...
So, to read a whole line of input and store it in the string name
:
std::cout << "Please enter your name:\n";
std::string name;
std::getline( cin, name );
As for reading numbers from input, I suggest the same approach in C++ as I suggest in C: Read a full line, and then parse it in memory. Error conditions are much easier to handle this way:
std::cout << "Please enter a number:\n";
std::string input;
std::getline( cin, input );
double number = 0.0;
try {
number = std::stod( input );
}
catch ( std::invalid_argument const & ex ) {
// input was not a number, do something about it
}
The above code needs the includes <string>
and <stdexcept>
.
Upvotes: 2
Reputation: 332
First of all, a name should be a string, or a character array, and the way you define as C-style is:
using namespace std;
maxNumOfCharacters = 10
char name[maxNumofCharacters];
Then how you input a character is as following:
#include <iostream>
cin.getline(name, maxNumofCharacters);
cout << "Name:" << name << "\n";
Also you can define the name as string, you can include string library:
#include <string>
string name;
If you use string library, the way you extract is with stringstream() command.
cout << "Enter Name:";
getline(cin, name);
Hope this helps.
EDIT: I found this tutorial and add this link so you can go and refer to how to define multiple inputs. Also check the general variable types in C++.
http://www.cplusplus.com/doc/tutorial/basic_io/
Upvotes: 3
Reputation: 11237
The way you should read a string in C++ is to use cin.getline
char name[32];
std::cout << "Enter the name\n";
std::cin.getline(name, 32);
Also, name
should be a char[]
not a double
.
Upvotes: 0