I am trying to get a function to store a users name that they have input but I am having difficulty doing so. Here is the code I have:
#include <iostream>
using namespace std;
int greeting()
{
var custName;
cout << "Welcome to Banking Inc.\n";
cout << "Can I please get your name?\n";
cin >> custName;//How can I store this input data?
cout << "Alright, " << custName << " lets get started.";
return custName;
}
int main()
{
greeting();
return 0;
}
When I run this, it just returns to me as the name of "0" instead of the name the user had typed. please help:(
venkatam
09-06-2009, 03:03 PM
Try "return custName;" before:
cout << "Alright, " << custName << " lets get started.";
So its:
#include <iostream>
using namespace std;
int greeting()
{
var custName;
cout << "Welcome to Banking Inc.\n";
cout << "Can I please get your name?\n";
cin >> custName;
return custName;
cout << "Alright, " << custName << " lets get started.";
}
int main()
{
greeting();
return 0;
}
UNIXy
09-06-2009, 03:09 PM
I'm surprised your code compiles at all. "var" is not a C++ native type. Here's the updated code:
#include <iostream>
using namespace std;
string greeting()
{
string custName;
cout << "Welcome to Banking Inc.\n";
cout << "Can I please get your name?\n";
cin >> custName;//How can I store this input data?
cout << "Alright, " << custName << " lets get started.";
return custName;
}
int main()
{
greeting();
return 0;
}
venkatam
09-06-2009, 03:12 PM
i could probably code this in java. haha
mattle
09-06-2009, 05:42 PM
Been a while for me and C++, but I think you'll have to include string.h to use the type string. A char array should be fine, however.
Either way, when you say When I run this, it just returns to me as the name of "0" instead of the name the user had typed. I'm not sure that you exactly know what you're saying here. The function is both returning and displaying the variable custName. You are however not doing anything with the return value of the function--thereby leading me to the question of how do you know what "it" is returning. Assuming you want to hold on to the customer name after you leave the scope of that first function, you need to assign the function's return value to a local variable in main(), or better yet, define the variable in main() and pass by reference to the function.
petteyg359
09-06-2009, 06:16 PM
#include <string>
ANSI C++ says you don't include .h for includes.
#include <iostream>
#include <string> // You can't use strings without including string...
using namespace std;
string /*NOT int*/ greeting() // If you want it to return a string, why are you telling it you want an int?
{
var custName; There is no type "var". C++ ain't PHP. Try string custName;
cout << "Welcome to Banking Inc.\n";
cout << "Can I please get your name?\n";
cin >> custName;//How can I store this input data?
cout << "Alright, " << custName << " lets get started.";
return custName;
}
int main()
{
greeting();
return 0;
}
thanks for the replies. I had another question though. How would you store a phone number with a "-" in the middle of the numbers too?
Codelphious
09-07-2009, 12:57 PM
This looks like a school project. There's no way you're building banking software (at least I hope not).
The best advice I could ever give you is to read your text book, write bad code, get a bad grade, write better code, get a better grade, and continue.
mattle
09-07-2009, 01:36 PM
thanks for the replies. I had another question though. How would you store a phone number with a "-" in the middle of the numbers too?
As a struct :)