nter first and surname and then display the full name problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: nter first and surname and then display the full name problem

  1. #1
    Join Date
    Sep 2012
    Posts
    17

    Post nter first and surname and then display the full name problem

    Hi

    I'am new to C++, i done various basic programs in C++, but one program is giving me a major headache: enter first and surname and then display the full name:

    code below:

    // Enter first and surname and then display the full name.
    #include<iostream>
    #include<string>
    #include<cctype>

    using namespace std;

    int main()
    {
    string first_name, surname;

    int main()

    {
    inp_string
    Pr_string

    return 0;

    }
    void inp_string
    {
    cout << "Enter your first_name";
    // cin >> first name;
    getline(cin,first_name); // reads all the line plus spaces
    cout << "enter your surname";
    // cin >> surname;
    getline(cin, suranme); // reads all the line plus spaces
    system ("CLS");
    }
    void pr_string

    {
    cout << "My full name is " << name << "\n";
    }

  2. #2
    Join Date
    Aug 2009
    Posts
    439

    Re: nter first and surname and then display the full name problem

    Here's the code with code tags (please use code tags in the future):
    Code:
    // Enter first and surname and then display the full name.
    #include<iostream>
    #include<string>
    #include<cctype>
    
    using namespace std;
    
    int main()
    {
    string first_name, surname;
    
    int main()
    {
        inp_string
        Pr_string
    
        return 0;
    }
    
    void inp_string
    {
        cout << "Enter your first_name";
        // cin >> first name;
        getline(cin,first_name); // reads all the line plus spaces
        cout << "enter your surname";
        // cin >> surname;
        getline(cin, suranme); // reads all the line plus spaces
        system ("CLS");
    }
    
    void pr_string
    {
        cout << "My full name is " << name << "\n";
    }
    Now, there is a lot wrong with your program. As it stands it will not compile. You reference two main functions. You aren't calling your functions correctly. Even if you were calling them correctly:

    Code:
    inp_string();
    pr_string();
    That would still not work. You need to declare your functions before using them. You can then define them after main() like you have now.

    So:

    Code:
    void inp_string();
    void pr_string();
    
    int main()
    {
        inp_string();
        pr_string();
    
        return 0;
    }
    
    void inp_string()
    {
         //definition here
    }
    
    void pr_string()
    {
         //definition here
    }
    On top of that, you have a spelling mistake in inp_string(). In pr_string() you have an undefined variable (name). At this point I would probably start over. If you are using a textbook, reread the sections on functions and input.

  3. #3
    Join Date
    Sep 2012
    Posts
    17

    Re: nter first and surname and then display the full name problem

    ok i understabd a little bit, if you were to right the same program, what would your code be?

  4. #4
    Join Date
    Aug 2009
    Posts
    439

    Re: nter first and surname and then display the full name problem

    It is again forum policy for us to do your homework. If I posted what I would write, then you would have the answer and not learn anything.

  5. #5
    Join Date
    Sep 2012
    Posts
    17

    Re: enter first and surname and then display the full name problem

    Ok made some changes when i press ctrl and f5 runs program asks for my first_name then surname then i press enter system says name isfirst_namesurname

    PHP Code:
    // Enter first and surname and then display the full name.
    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;

    string first_namesurnamename;
    void in_string();
    void pr_string();

    int main()
    {
        
    in_string();
        
    pr_string();

        return 
    0;
    }

    void in_string()
    {
        
    cout << "enter your first_name";
        
    // cin >> first name;
        
    getline(cin,first_name); // reads all the line plus spaces
        
    cout << "enter your surname";
        
    // cin >> surname;
        
    getline(cinsurname); // // reads all the line plus spaces
        
    system ("CLS");
    }

    void pr_string()
    {
        
    cout << "name is" << "first_name" << "surname" << name << "\n";


  6. #6
    Join Date
    Sep 2012
    Posts
    17

    Re: enter first and surname and then display the full name problem

    ok it works now only one wee minor problem, when you enter your first name then enter surname enter the full name is displayed, but no space in between first and surname, example johnsmith should be john smith with a space in the middle

    PHP Code:
    // Enter first and surname and th2; display the full name.
    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;

    string first_namesurnamename;
    void in_string();
    void pr_string();

    int main()
    {
        
    in_string();
        
    pr_string();

        return 
    0;
    }

    void in_string()
    {
        
    cout << "enter your first_name: ";
        
    // cin >> first name;
        
    getline(cin,first_name); // reads all the line plus spaces
        
    cout << "enter your surname: ";
        
    // cin >> surname;
        
    getline(cinsurname); // // reads all the line plus spaces
        
    system ("CLS");
    }
    void pr_string()
    {
       
    cout << "name is " << first_name << surname << name  << "\n"


  7. #7
    Join Date
    Sep 2012
    Posts
    17

    Re: nter first and surname and then display the full name problem

    still can not get space in between the first and surname

    PHP Code:
    // Enter first and surname and then display the full name.
    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;

    string first_namesurnamename;
    void in_string();
    void pr_string();

    int main()
    {
        
    in_string();
        
    pr_string();

        return 
    0;
    }

    void in_string()
    {
        
    cout << "enter your first_name: ";
        
    // cin >> first name;
        
    getline(cinfirst_name); // reads all the line plus spaces
        
    cout << "enter your surname: ";
        
    // cin >> surname;
        
    getline(cinsurname); // // reads all the line plus spaces
        
    system ("CLS");
    }
    void pr_string()
    {
       
    cout << "my name is "  << first_name  << surname  << name  << "\n";


  8. #8
    Join Date
    Sep 2012
    Posts
    17

    Re: nter first and surname and then display the full name problem

    Problem fixed last line

    PHP Code:
    void pr_string()
    {
       
    cout << "my name is " << first_name << " " << surname << name << "\n";

    final code

    PHP Code:
    // Enter first and surname and then display the full name.
    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;

    string first_namesurnamename;
    void in_string();
    void pr_string();

    int main()
    {
        
    in_string();
        
    pr_string();

        return 
    0;
    }

    void in_string()
    {
        
    cout << "enter your first_name: ";
        
    // cin >> first name;
        
    getline(cin,first_name); // reads all the line plus spaces
        
    cout << "enter your surname: ";
        
    // cin >> surname;
        
    getline(cin,surname); // reads all the line plus spaces
        
    system ("CLS");
    }
    void pr_string()
    {
       
    cout << "my name is " << first_name << " " << surname << name << "\n";


  9. #9
    Join Date
    Aug 2009
    Posts
    439

    Re: nter first and surname and then display the full name problem

    I want to point out that name is still undefined. You declare it but never set it equal to anything, so it's contents are garbage. Since you had trouble with this, I think it might be a good exercise to play around with it. Get rid of all the global variables. Pass a string parameter to your print function. The best way to learn something new about a language is to work with it, but with small example programs. Get a feel for how that something works with a short, simple and easy to understand program, and then proceed to use it in a larger program. Hope this helps.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    Re: nter first and surname and then display the full name problem

    Quote Originally Posted by Alterah View Post
    I want to point out that name is still undefined. You declare it but never set it equal to anything, so it's contents are garbage. Since you had trouble with this, I think it might be a good exercise to play around with it. Get rid of all the global variables. Pass a string parameter to your print function. The best way to learn something new about a language is to work with it, but with small example programs. Get a feel for how that something works with a short, simple and easy to understand program, and then proceed to use it in a larger program. Hope this helps.
    You're right that name is being output without ever being given a value, but wrong that it's contents are garbage. A string will be empty when it's declared.

  11. #11
    Join Date
    Aug 2009
    Posts
    439

    Re: nter first and surname and then display the full name problem

    Quote Originally Posted by GCDEF View Post
    You're right that name is being output without ever being given a value, but wrong that it's contents are garbage. A string will be empty when it's declared.
    Whoops. Thanks for pointing that out.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center