# noob here with questions

• 03-02-2010
rllove37
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
• 03-02-2010
Daved
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?
• 03-02-2010
Quote:

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).

Quote:

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.
• 03-02-2010
rllove37
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
• 03-02-2010
hk_mp5kpdw
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.
• 03-02-2010
rllove37
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?
• 03-02-2010
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.
• 03-02-2010
Daved
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?
• 03-02-2010
rllove37
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
• 03-02-2010
Daved
>> 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?
• 03-02-2010
hk_mp5kpdw
Quote:

Originally Posted by Daved
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.
• 03-02-2010
Quote:

Originally Posted by rllove37
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.
• 03-02-2010
rllove37
Quote: