I'm new to C++ and need basic help please

This is a discussion on I'm new to C++ and need basic help please within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by SkinnieMinnie MacGyver U were the exact person I had in mind when I ever so carefully typed ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by SkinnieMinnie View Post
    MacGyver
    U were the exact person I had in mind when I ever so carefully typed my original question.
    /me tips his hat.

    Quote Originally Posted by SkinnieMinnie View Post
    I'm not a slacker and I do not simply cheat or copy the work of others, so I can understand why you wrote what you did.
    I believe you meant "can't" instead of "can". I wrote what I did because of this:

    Quote Originally Posted by SkinnieMinnie View Post
    really I guess I should have said give me the answer lol lol!!
    We get a lot of people here that are really like that. Don't take my comment so seriously. I was just passing on a friendly warning to keep active. According to a section of a well known web page you should try to demonstrate you did a little bit of research in response to the help you get.

    Again, don't take any of this personally, because it isn't.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    ..

    Ok, thanks, won't take it personally then.....nope I did mean that I can understand.......since I'm not a slacker and put a lot of work into what I turn in I can understand why u would make that comment.....I have a problem with those not willing to do the work and just trying to copy as well.....as far as showing my effort.....I will make sure to do that......the whole give me the answer thing has the lol lol lol after it.....lol as i was actually joking and even chuckling a little......I then went on to explain how I still didn't understand.....thank u for your help....I wasn't aware of the "well known web page" so rest assured that I have now reviewed it and see that there is/was a lot that I am/was ignorant to....thank you
    Last edited by SkinnieMinnie; 09-08-2007 at 10:10 AM.

  3. #18
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Hope everything works out with this exercise that you're working on.

    And, of course, welcome to cboard.

  4. #19
    Registered User
    Join Date
    Sep 2007
    Location
    World
    Posts
    2
    It will work if u have 4 digits only.

  5. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Yeah I finally got it!! Thank you everyone for your help!! Here is my finished product.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main( )
    {
    int string;
    int a;
    int b;
    int c;
    int d; 
    int e;
    int f; 
    
    cout<< "Please enter a four-digit positive integer and "
        << "press the enter key." << endl; 
    
    cin >> string;
    
    cout << endl;
    
    //This will print the first number.
    a = string / 1000;
    
    cout << a;
    cout << endl; 
    
    //This calculation will provide the second number.
    b = string&#37;1000; 
    c = b / 100;  
    
    cout << c;
    cout << endl;
    
    //This calculation will provide the third number.
    d = b%100; 
    e = d / 10; 
    
    cout << e;
    cout << endl;
    
    //This calcuation will provide the fourth number.
    f = d%10;
    
    cout << f; 
    
    cout << endl; 
    
        return 0;
    }
    Last edited by SkinnieMinnie; 09-08-2007 at 09:03 PM.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yup good job. my only comment would be to indent everything in the same block to the same level. what i mean is, everything after the '{' (after "int main()") tab over once, like you have for the 'return 0;'. when they are all lined up it is much easier to read as it shows what code is in what 'level'. im sure youll see and learn that this is very helpful as you continue to program.

    as an exercise, you could try and rewrite so that it will work for any positive integer (ie numbers > 9999). youd have to grab some paper and draw/think out an algorithm that would work for any case. it would be good practice.
    Last edited by nadroj; 09-08-2007 at 06:28 PM.

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> int string;
    Don't name your variable string. It is not a string, it is a number. Your solution didn't use strings, which I think is a good thing. That variable is the user input, so I would name it input, or user_input or number, or something more meaningful and correct.

  8. #23
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    ..

    ok, thanks!

    Code:
    #include <iostream>
    using namespace std;
    
    int main( )
    {
    int input;
    int a;
    int b;
    int c;
    int d; 
    int e;
    int f; 
    
    cout<< "Please enter a four-digit positive integer and "
        << "press the enter key." << endl; 
    
    cin >> input;
    
    cout << endl;
    
    //This will print the first number.
    a = input / 1000;
    
    cout << a;
    cout << endl; 
    
    //This calculation will provide the second number.
    b = input&#37;1000; 
    c = b / 100;  
    
    cout << c;
    cout << endl;
    
    //This calculation will provide the third number.
    d = b%100; 
    e = d / 10; 
    
    cout << e;
    cout << endl;
    
    //This calcuation will provide the fourth number.
    f = d%10;
    
    cout << f; 
    
    cout << endl; 
    
        return 0;
    }

  9. #24
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Here is the way another person in my class was able to get the string idea to work:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main( )
    {
    	string numStr;
    
    	cout << "Enter a four digit positive integer. ";
    
    	cin >> numStr;
    	cout << endl; 
    
    	cout << numStr[0] << endl; 
    	cout << endl; 
    
    	cout << numStr[1] << endl; 
    	cout << endl; 
    
    	cout << numStr[2] << endl; 
    	cout << endl;
    
    	cout << numStr[3] << endl; 
    	cout << endl; 
    	
    	return 0;
    }

  10. #25
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That works too in theory, but there's no checking done to see whether what's entered is actually a number

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #26
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i was going to mention that, too. however in the op's final code there is no checking either, so both would fail in the case that the user enters something but an integer.

    i hope the op realizes that he/she will be a better programmer than the other student in that class in the future if they continue to program in a way that involves thinking outside the box. the other students' code works (aside from the error checking of course) but im sure they learned very little or nothing.

  12. #27
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by nadroj View Post
    in the op's final code there is no checking either
    Technically, becuase in SkinnieMinnie's code, 'input' is declared as an int, cin would convert the input into an int (I may be wrong). So if you enter garbage, you'll likely only get 0. Which you can divide to your heart's content ;-)

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Since the input variable is uninitialized, if you enter garbage it will remain unitialized and you will get some seemingly random number, it will not be set to 0. If you were to initialize it to 0 in the first place, which is good practice, then a failed read would leave it as 0 and the program wouldn't output an undefined answer.

  14. #29
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Daved View Post
    Since the input variable is uninitialized, if you enter garbage it will remain unitialized and you will get some seemingly random number, it will not be set to 0. If you were to initialize it to 0 in the first place, which is good practice, then a failed read would leave it as 0 and the program wouldn't output an undefined answer.
    Awesome, I'm going to use that as an example from now on for why people should *always* initialise their variables. Thanks!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21