2
#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

DevSolar
  • 67,862
  • 21
  • 134
  • 209
acm818
  • 159
  • 1
  • 7
  • 3
    Are you trying to enter characters for the `name`? If so `name` should not be a `double`. – NathanOliver Feb 18 '16 at 16:26
  • 3
    Guys, why are you downvoting this? This is probably one of his first programs after joining ACM by the looks of his username. debugging is a skill that is acquired through experience. Don't scare him off from asking for help by downvoting. @acm818, try changing name to a string. A double only contains a decimal value like -12.454 while a string contains characters like "Reiss Bachman". – joshualan Feb 18 '16 at 16:37
  • Yes i was. Thanks for the help man. you pointed me in the right direction and i got it working now – acm818 Feb 18 '16 at 16:42

3 Answers3

3

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/

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
kukushkin
  • 312
  • 4
  • 16
2

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>.

Community
  • 1
  • 1
DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

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.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
  • Well, actually it should be a `std::string`. Don't teach the use of `char[]` in C++. – DevSolar Feb 18 '16 at 16:37
  • @DevSolar `getline` takes in a char array, not `std::string`. Besides, `char[]` isn't that bad once you're used to it. – lost_in_the_source Feb 18 '16 at 16:38
  • Shouldn't we be trying to use C++ functionality to make his life easier instead of C stuff? Granted, you can pass in a char[] to a string constructor but it might be easier to simply only do string stuff. – joshualan Feb 18 '16 at 16:42
  • 2
    The *real* `getline` [does take `std::string`](http://en.cppreference.com/w/cpp/string/basic_string/getline), and yes, `char[]` is just **that** bad. – DevSolar Feb 18 '16 at 16:42
  • Isn't that obvious from the comments? Showcasing bad practice is not helpful. – DevSolar Feb 18 '16 at 16:48
  • Just because I used a different method than you'd like, doesn't mean I deserve a downvote. The issue of `char *` vs `std::string` is opinion. Your downvote is like downvoting a post that uses a brace style that you don't like. @DevSolar – lost_in_the_source Feb 18 '16 at 16:50
  • My issues are you are using a magic numbers so if you change one you have to change the other, `string` only uses the size it needs where a fixed char array can be a waste of space and if the user happens to enter more then what you allowed for space now you have trimmed the users input and you leave content in the stream. – NathanOliver Feb 18 '16 at 17:04
  • Well, I disagree, with the statement of your answer (`cin.getline` being "the way you should read a string in C++"), with your statement that `char[]` is "not that bad", that C arrays vs. C++ containers is an "issue of opinion", *and* that this were on the level of disputing brace style. I'd point that piece of code out in every code review we'd be doing, and insist on proper use of C++ idioms. I could also tell you of the number of `char[]`-related errors by people who *thought* they got it right, which I had to fix over the years... You have your opinion, I have mine, sorry. @stackptr – DevSolar Feb 18 '16 at 17:24
  • @DevSolar that's still like downvoting code that uses the K&R brace style, because you might add a line inside the block, but forget to put braces surrounding it, causing subtle bugs. – lost_in_the_source Feb 18 '16 at 17:50
  • Well then, tell me what happens when the OP uses your code verbatim, and the user enters a name of 33 characters? What if the name is *exactly* 32 characters? How to cater for those conditions? *C arrays are a relict*. – DevSolar Feb 18 '16 at 18:02
  • @DevSolar The OP can allocate enough memory. The OP is getting a name from the user . It's unlikely that the name will be longer than 20 characters, for example. So, he can allocate 21 characters (+1 for the null terminator). – lost_in_the_source Feb 18 '16 at 18:06
  • My daughter's full name has 27 characters. Spanish full names tend to be significantly longer still. And the simplest solution is to use `std::string` which can take *any* length, but I guess you have dug in too deep by now to acknowledge that. – DevSolar Feb 18 '16 at 18:10
  • @DevSolar I was talking about first names. I assume that's what the OP meant when he said "name" in his question. – lost_in_the_source Feb 18 '16 at 18:33
  • You are *still* argueing? The only reason the member version of `getline()` *exists* in the first place is because `` *predates* ``. That is why `fstream::open()` takes a C string instead of a C++ one. Strings and vectors are the two complete no-brainers in the C++ toolkit. No assumption on the input length will ever be "safe" *and* "efficient". – DevSolar Feb 18 '16 at 18:43