Reputation: 1
I'm am a beginner in C++. I'm using Xcode to compile my code. Right now I am going over variables and doing a brief exercise on the subject. The exercise asks that I ask for the user to input their first and last name as well their age. As a additional requirement I need to use a double floating point for age so I can multiply the age into months. Below is my code:
#include <iostream>
#include <math.h>
int main()
{
std::cout << "Please enter your first name, second name, and age (then press enter).\n";
std::string first;
std::string last;
std::double age;
std::cin >> first >> last >> age;
std::cout << "Hello, " << first << " " << last << ". Your age is " << age << " and you are " << (age * 12) << " months old.";
return 0;
}
I get an error that says that the double is an expected unqualified-id. Can someone point out what I'm doing wrong and the correct way to do this?
Upvotes: 0
Views: 1033
Reputation: 145359
I believe the code below is about ideal for learning how to do this kind of nearly-first C++ program.
The little technical problem with your code was just that double
is keyword, not a name defined by the standard library, and hence, not in namespace std
.
In addition I’ve added
inclusion of the <string>
header, necessary for using std::string
in a portable way,
a using namespace std;
directive, very handy for small exploration programs (but do not place this in the global namespace in a header file!),
checking of whether input operations succeed (also output can fail but that’s extremely rare).
The way that I check for input operation failure, using boolean "or" (the ||
operator), is not yet very much used in C++, but is common in some other languages. Essentially the left hand argument of ||
is converted to bool
, since that’s what ||
requires. The left hand argument is the expression result of some input operation, which in general is a reference to the cin
stream, and a bool
value is then produced via a defined conversion that is equivalent to writing !cin.fail()
(where !
is the logical "not" operation).
E.g., getline( cin, first ) || fail( ... )
reads very nicely as “getline
or else fail
”, and in addition to reading nicely it’s also visually distinctive, easy to recognize as a failure check.
#include <iostream>
#include <string>
#include <stdlib.h> // exit, EXIT_FAILURE
using namespace std;
// Poor man's way to handle failure, but good enough here:
bool fail( string const& message )
{
cerr << "!" << message << endl;
exit( EXIT_FAILURE );
}
int main()
{
cout << "Please enter your first name: ";
string first;
getline( cin, first )
|| fail( "Sorry, input of your first name failed." );
cout << "Please enter your last name: ";
string last;
getline( cin, first )
|| fail( "Sorry, input of your last name failed." );
cout << "Please enter your age in years: ";
double age;
cin >> age
|| fail( "Sorry, input of your age failed." );
cout << "Hello, " << first << " " << last << "." << endl;
cout
<< "Your age is " << age << " years"
<< " and you are "<< (age*12) << " months old."
<< endl;
}
Upvotes: 0
Reputation: 311058
First of all it is a good idea to include header <string>
#include <string>
If in C# so-called built-in types in realty are aliases for classes as for example double is an alias for System.Double and you can write
System.Double age;
or
double age;
in C++ these types indeed are built in types and use keywords as double
to specify a type
double age;
Though i do not understand why age should be double because the number of months in year is an integer value.:)
Upvotes: 0
Reputation: 153955
double
is a built-in type. It doesn't live in any namespace and doesn't need any qualification! Just remove the std::
in front of double
:
double age;
Note, you should test whether your input was actually successful:
if (std::cin >> first >> last >> age) {
// process successful input
}
else {
std::cout << "ERROR: failed to read expected input\n";
}
Upvotes: 2
Reputation: 227468
double
does not live in the std
namespace. You need
double age;
You also need to include the string
header for std::string
. You may get it indirectly from iostream
on some implementations, but you cannot rely on that: it is a fluke.
Upvotes: 4