Results 1 to 9 of 9

Thread: c++ help please

  1. #1
    Join Date
    May 2008
    Posts
    65

    c++ help please

    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:
    Code:
    #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

  2. #2
    Join Date
    Oct 2007
    Posts
    239
    Try "return custName;" before:

    cout << "Alright, " << custName << " lets get started.";

    So its:

    Code:
    #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;
    }

  3. #3
    Join Date
    Feb 2008
    Location
    Houston, Texas, USA
    Posts
    2,955
    I'm surprised your code compiles at all. "var" is not a C++ native type. Here's the updated code:
    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;
    }
    UNIXy - Fully Managed Servers and Clusters - Established in 2006
    [ cPanel Varnish Nginx Plugin ] - Enhance LiteSpeed and Apache Performance
    www.unixy.net - Los Angeles | Houston | Atlanta | Rotterdam
    Love to help pro bono (time permitting). joe > unixy.net

  4. #4
    Join Date
    Oct 2007
    Posts
    239
    i could probably code this in java. haha

  5. #5
    Join Date
    May 2009
    Posts
    766
    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.

  6. #6
    Join Date
    Mar 2009
    Posts
    649
    #include <string>

    ANSI C++ says you don't include .h for includes.

    Code:
    #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;
    }
    [GB ≠ GiB] [MB ≠ MiB] [kB ≠ kiB] [1000 ≠ 1024] [Giga ≠ gram] [Mega ≠ milli] [Kelvin ≠ kilo] [Byte ≠ bit]
    There is no millibit. There is no gram-bit. There is no Kelvin-Byte.

  7. #7
    Join Date
    May 2008
    Posts
    65
    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?

  8. #8
    Join Date
    May 2007
    Location
    Orange Country, CA
    Posts
    138
    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.
    Blesta - Professional Billing Software
    We are about creating good experiences
    Trial - Demo | 866.478.7567 | Twitter @blesta

  9. #9
    Join Date
    May 2009
    Posts
    766
    Quote Originally Posted by luxx View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •