Learning cplusplus, trubles! menu

Shout-Out

User Tag List

Results 1 to 6 of 6
  1. #1
    kunimitzu's Avatar Member
    Reputation
    49
    Join Date
    Dec 2007
    Posts
    82
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Learning cplusplus, trubles!

    So i am learning c++ and i found a rather strange problem.
    been re-reading my code several times to try and find whats the problem, without any luck i have to say.
    Now i came here to see if someone could help me out, and explain this for me, as i know it will be a problem down the road even if it is not now.

    Learning cplusplus, trubles!-tutorial_problem_1-png

    this is how a test run looks like and if you read through it you will see that in firstname "one" is missing.
    why does it remove the first characters until a space ? if the name were "one" it would output " "

    here is my code
    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        int numbervariable;
        cout << "How Old Are You?\n I am : ";
        cin >> numbervariable;
    
    
    //char give single character and string allows several, but does not allow spaces
        string firstname;
        cout << "What is your first name?\n My first name is : ";
        cin >> firstname;
    //for some reason this will blank out the first part of the name; "tom = " "
        getline ( cin, firstname, '\n' );
    //without it the application will skip all cin and rush through the process if the name entered is "first middle" or "first - middle" form.
        string lastname;
        cout << "What is your last name?\n My last name is : ";
        cin >> lastname;
        string charactervariable = firstname + " " + lastname;
    
    
        double decvar;
        cout << "How much do you weight in kilograms?\nI weight : ";
        cin >> decvar;
    
    
        cout << "Your name is : " << charactervariable << ".\nYou are : " << numbervariable << " Years old." << "\nYour weight is : " << decvar << "kg." << "\n \n";
    
    
        int numplus = 0;
        cout << ++numplus;
    
    
        return 0;
    }
    Thanks in advance.
    - kun
    Last edited by kunimitzu; 02-13-2014 at 04:59 AM.

    Learning cplusplus, trubles!
  2. #2
    xalcon's Avatar Contributor ふたなり
    Authenticator enabled
    Reputation
    198
    Join Date
    Oct 2008
    Posts
    291
    Thanks G/R
    20/58
    Trade Feedback
    0 (0%)
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You cannot use the stream syntax with cin in this szenario. When streaming into cin the space character is used as a delimiter.
    The reason you get just the leading parts after a space is simple:
    You read the first word until the first occuring space. Then you use getline() to read the full line, but the carret is already set to the second word (due to the cin stream behavior), overriding the first word in firstname with the leading part.

    In order to get you program working, you need to replace all "cin >> string" calls with getline(). To read into other types then string (like int or double) you need to read the remaining stuff fromt he line (the newline character, for example). I've created a "garbage" string for this purpose, but there are many ways to solve this problem.
    Just an working example:
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
    	string garbage;
        int numbervariable;
        cout << "How Old Are You?\n I am : ";
        cin >> numbervariable;
    	// we only got the number, but there is still a newline character we need to skip
    	getline(cin, garbage);
    
    
        string firstname;
        cout << "What is your first name?\n My first name is : ";
        getline (cin, firstname, '\n');
    
    
        string lastname;
        cout << "What is your last name?\n My last name is : ";
        getline (cin, lastname, '\n');
        string charactervariable = firstname + " " + lastname;
    
    
    	double decvar;
        cout << "How much do you weight in kilograms?\nI weight : ";
    	cin >> decvar;
    	// same as for the numbervariable above, read the newline character
    	getline(cin, garbage);
    
    
        cout << "Your name is : " << charactervariable << ".\nYou are : " << numbervariable << " Years old." << "\nYour weight is : " << decvar << "kg." << "\n \n";
    
    
        int numplus = 0;
        cout << ++numplus;
    
    
        return 0;
    }
    output:
    How Old Are You?
    I am : 123
    What is your first name?
    My first name is : Abc Defg
    What is your last name?
    My last name is : van long name
    How much do you weight in kilograms?
    I weight : 1337
    Your name is : Abc Defg van long name.
    You are : 123 Years old.
    Your weight is : 1337kg.

    1
    "Threads should always commit suicide - they should never be murdered" - DirectX SDK

  3. #3
    kunimitzu's Avatar Member
    Reputation
    49
    Join Date
    Dec 2007
    Posts
    82
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, especially for explaining as well as giving example.
    now i am able to continue my learning book, did not want go ahead without understanding everything in the previous chapter.
    i am sure i will have more simple, like this, and advanced problems later in my learning.

  4. #4
    kunimitzu's Avatar Member
    Reputation
    49
    Join Date
    Dec 2007
    Posts
    82
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    yesno

    now i made another simple tutorial program, this time it ran as expected and everything worked fine, however it gave me 2 compile warnings.

    Learning cplusplus, trubles!-tutorial_problem_2-jpg

    so my question here would be, are these warnings something i should pay attention to ?
    should i bother fixing it, rather than ignoring it when the program work as expected.

    also note; the two lines were "numbercheck" is used, i found to be necessary for the program to run as intended,
    otherwise, without me declaring it outside "while 1" it would get an error and not run.


    EDIT: Solved these warnings by moving the whole "if bool, else" part into the proper "if password" section, which also allowed me to remove the first declaration of "numbercheck"

    also found that the first code, in img, would give me some problems if i navigated deep enough into the program.

    so for others who want to learn, the picture is how its not done.

    here is the working code for this program.
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        string password = "Test";
        string userinput;
        cout << "What is the correct password ? \n Password : ";
        getline( cin, userinput, '\n' );
    
    
        while (1)
            {
    
    
                if (userinput == password)
                    {
                        cout << "\nCorrect password was entered \n";
                        return 0;
                        cin.get();
                    }
    
    
                else if (userinput == "Hacked")
                    {
                        int number;
                        cout << "\nPassword Hacked ! \n Type a number to enter : ";
                        cin >> number;
                        bool numbercheck = number == 2;
    
    
    
    
                    if ( numbercheck )
                        {
                            cout << "\nCorrect number was entered ! \n";
                            cin.get();
                            return 0;
                        }
                    else
                        {
                            cout << "\nWrong number ! Try again ! \n";
                            cin.get();
                        }
    
    
                    }
    
    
                else
                    {
                            cout << "\nWrong password was entered \nTry again : ";
                            getline( cin, userinput, '\n');
    
    
                    }
            }
    }
    Last edited by kunimitzu; 02-13-2014 at 06:58 PM.

  5. #5
    stoneharry's Avatar Moderator Harry


    Reputation
    1618
    Join Date
    Sep 2007
    Posts
    4,564
    Thanks G/R
    151/150
    Trade Feedback
    0 (0%)
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Warnings are exactly what they are - warnings. The compiler and/or your IDE is telling you this might be an issue.

    In your screen shot it looks like one variable is not given an initial value, this will error if you try to use it and it doesn't have a value. Im sure your book will go into more details about them later.

    Posted from phone so hard to answer.

  6. #6
    kunimitzu's Avatar Member
    Reputation
    49
    Join Date
    Dec 2007
    Posts
    82
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solved it by doing some more trails and error, but thank you anyway.
    also as u said, the book went more into warnings and compilers and such.

    Edit: Solved next problem.

    -Kun
    Last edited by kunimitzu; 02-18-2014 at 12:20 PM.

Similar Threads

  1. is your MWS crashing when u click on an M2? Learn how to fix it here.
    By soulcatcher in forum World of Warcraft Model Editing
    Replies: 17
    Last Post: 01-06-2007, 10:29 AM
  2. Want to learn.... please help
    By Kelindel in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 01-02-2007, 06:30 PM
  3. just wanna learn
    By stevendude in forum World of Warcraft General
    Replies: 10
    Last Post: 10-28-2006, 10:13 AM
  4. just wanna learn
    By stevendude in forum World of Warcraft Bots and Programs
    Replies: 10
    Last Post: 10-28-2006, 10:13 AM
  5. Pet Skills List - Where to learn pet skills
    By Breeze in forum World of Warcraft Guides
    Replies: 3
    Last Post: 09-23-2006, 08:54 AM
All times are GMT -5. The time now is 11:00 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search