Thread: noob here with questions

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    noob here with questions

    I need some help with my homework, I've been taking a c++ course for like 7 weeks now and have been running into some problems that the teacher hasn't exactly been covering in class, my first program is just supposed to be a simple switch program, for some reason it's not solving it, I'm sure its something stupid I'm not seeing
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char opselect;
        double fnum, snum;
        
        cout << "Please type in two numbers ";
        cin  >> fnum >> snum;
        cout << "Please select a code: ";
        cout << "\n     a add";
        cout << "\n     b multiply";
        cout << "\n     c divide";
        cout << "\n";
        cin  >> opselect;
    
        
        switch (opselect)
        {
               case 'a':
                    cout << "The sum of the numbers entered is " << fnum+snum << endl;
                    break;
               case 'b':
                    cout << "The product of the numbers is " << fnum*snum << endl;
                    break;
               case 'c':
                    cout << "The dividend of the numbers is" << fnum/snum << endl;
                    break;
                    
        }
    
        system("pause");
        return 0;
        
    }
    also I can't just use int opselect cause we have to use char for some reason, the next thing is a little weirder i guess, we never covered this, we are supposed to put an algorithm into a program, it has 4 steps to convert a numeric value to a certain amount of decimal places, but step three is to get rid of all decimal places, I don't know how I would do that in c++

    Code:
     #include <iostream>
    using namespace std;
    
    int main()
    {
        double money, interest, total;
    
    
        cout << "Enter an amount of money: ";
        cin  >> money;
        
        intrest = 0.08675;
        total = money * interest;
        
        total = total * (10*10); // step one
        total = total + 0.5;        // step two
                                             // step three would be to drop all decimal places from total
        total = total / (10*10); // step four finishes it
        
        cout << "The interest is: " << total << endl;
        system("pause");
        return 0;
        
    }
    I hate to ask these questions but me and my friends cant really figure out how to do it, my teacher wasnt to much help and his response to second program was "just put it directly into the program, thats all i can really tell you" and thanks for any help in advance

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    For the first program, what's the problem? Is it giving incorrect output? Your use of opselect and the switch seem fine at a quick glance.

    For the second program, here's a hint: what's the difference between a double and an int?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    my first program is just supposed to be a simple switch program, for some reason it's not solving it
    I dont know what "its not solving it" means. I dont see a problem with it (besides the use of "system" and the possibility of runtime error due to dividing by 0).

    step three is to get rid of all decimal places, I don't know how I would do that
    If you have a double, say "double e = 2.71" and you want to "get rid of the decimal places" the easiest thing to do would be to cast it to a type that doesn't store decimal places. That is, you cast it to an integer by doing something like "(int)". Do a quick search on "casting" to see (and understand!) the full answer.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    the first program pretty much, I enter the numbers, and then it asks me to select a operation, i hit a b or c and then it just doesnt go to the operation, i tried it with int and 1/2/3 and it worked fine

    are you saying in the second program that I need to change the total to int half way through the program? cause what it is supposed to do is you enter a amount of money, lets say $10.25, then it times .08675 for interest and comes up with (lets just say) $1.22224, the algorithm is supposed to be like

    1.22224 * (10*10) = 122.224
    122.224 + 0.5 = 122.724
    drop the decimals = 122
    122 / (10*10) = $1.22

    I'm just confused on how to drop the decimals

    thanks for idea of casting, i have never even heard of that until now, i'll look it up, and as i said in this post by "its not solving it" i mean lets say i hit 5 = fnum 5 = snum and a for addition it just says press to a key continue and it exits. the system("pause") is cause I'm doing this on devc++ and the window doesn't stay open other wise
    Last edited by rllove37; 03-02-2010 at 03:06 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
        char opselect;
        double fnum, snum;
    
        cout << "Please type in two numbers ";
        cin  >> fnum >> snum;
        cout << "Please select a code: ";
        cout << "\n     a add";
        cout << "\n     b multiply";
        cout << "\n     c divide";
        cout << "\n";
        cin  >> opselect;
    The problem is probably that the last cin tries to get a character and the previous cin left the newline in the input stream. Reading a char does not skip the leading newline. If you put a case '\n' option in your code with a debug print statement you'd probably see the debug message get output. The solution to this is you need to either call the ignore member function or perhaps have a dummy cin.get() call to eat up that leftover newline in between those two cin calls.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    i didn't exactly get that? keep in mind I've only been doing this for a little over a month now, this is actually my 7th week

    edit : also having trouble with the casting, not to sure about that either. I saw like 4 different forms of it, reinterpret, static, const, and dynamic. Am I even looking at the right stuff?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    By "drop the decimals" you mean a number like 1.22 should be 1.00? If so, again, just cast it to an integer. Try the following snippet of code in a new "test" c++ program
    Code:
    double e = 2.17;
    // print "e" using cout
    e = (int) e;
    // print "e" using cout
    If you dont know the primitive data types, read a simple article/tutorial explaining them. "int" basically corresponds to the "integer" number. These are "whole" numbers, such as 0, 1, 42, -1234, "float" and "double" loosely corresponds to "real" numbers. These are (practically*) "all other numbers", such as 0, 1, 1.0, 1.1111, -42, -1234.5678. The set of integers is a subset of the real numbers, as you can see here, all "int"s can be considered as a "double". However, if you consider a "double" as an "int", you will be loosing the fractional part (part to the right of the decimal), and this will likely cause a warning or error by your compiler because its saying "Oops! You might be loosing information/precision about this number, I'll prevent that from happening." If you "cast" it to an integer then the compiler will let you "throw away" the fractional part.


    * Math people: I'm aware of imaginary numbers, but for the sake of simplicity I've excluded it in the discussion above... so you don't need to correct me.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Reading a char does not skip the leading newline.
    I don't think that's correct, hk_mp5kpdw. The operator>> will skip the preceding newline even if it is reading in a char.

    The code for the first program works or me. What exact input are you giving it, rllove37?

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    Hazaa I got the money one to work just did this
    Code:
     total = total * (10*10);
        total = total + 0.5;
        total = (int) total;
        total = total / (10*10);
    thanks on that one, but I am still not sure why the first one isn't working

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I am still not sure why the first one isn't working
    It works for me. Are you sure you built your changes and are using the latest version?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Daved View Post
    >> Reading a char does not skip the leading newline.
    I don't think that's correct, hk_mp5kpdw. The operator>> will skip the preceding newline even if it is reading in a char.

    The code for the first program works or me. What exact input are you giving it, rllove37?
    Well, he can put in the case '\n': with a debug print statement like I mentioned and see if something is happening there or not. It was something I thought was likely but maybe I'm wrong.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by rllove37 View Post
    Hazaa I got the money one to work just did this
    Code:
     total = total * (10*10);
        total = total + 0.5;
        total = (int) total;
        total = total / (10*10);
    thanks on that one, but I am still not sure why the first one isn't working
    I'd strongly recommend you understand why it works, before moving on and feeling proud . Seriously.

    As for the first one, Im not sure what you mean in your explanation of how you entered the numbers. Does the flow of the input/output look similar to this?
    Code:
    jordan@jordan-laptop:~$ ./yourprogram 
    Please type in two numbers 1 2
    Please select a code: 
         a add
         b multiply
         c divide
    a
    The sum of the numbers entered is 3
    Where red text indicates input.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    Quote Originally Posted by nadroj View Post
    I'd strongly recommend you understand why it works, before moving on and feeling proud . Seriously.
    i understand it, intergers have no decimal places, i just didn't know you could change it in the middle of a program like that.

    On the other program, apparently I still had opselct as an int... (facepalm) my bad

    edit: btw thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Few more noob questions
    By SlpCtrl in forum C Programming
    Replies: 12
    Last Post: 10-14-2008, 04:32 PM
  2. A bunch of REALLY uber noob questions
    By mcmasterballer in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 01:43 PM
  3. 2 very noob questions
    By chasingxsuns in forum C Programming
    Replies: 20
    Last Post: 05-07-2006, 11:04 PM
  4. Noob Questions...
    By Firemagic in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 03:57 PM
  5. just started using CPP, couple of arb NooB questions
    By sempuritoza in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 05:21 AM