"Cin" Issue solved, but new one arose.

Jun 22, 2013 at 12:31pm
THIS ISSUE IS SOLVED, the next one isn't

If I take out the cin (along with the digits variable) it will build and run fine, but if I put the cin in the code it will not build or run. I've never had this issue before and it's doing this with two programs: DEVC++ and CodeBlocks so I know it is something I'm doing wrong.

Help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>

using namespace std;

int main()
{
    short int number;
    number = 12;

    long int digits;

    cout << number << endl;
    cin >> digits >> endl;


    cout << "Hello world!" << endl;
    return 0;
}


I searched the internet but couldn't really find an answer.
SOLVED.

--------------------------------------------

New issue sadly. Didn't want to make a whole new thread for it.

Whenever I multiply my variable by a number, the product is weird. Example, I put in 10 for seconds and I should get out 5, but I get out 15149.5 Anyone know why this is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
    unsigned short int seconds;
    double burned= seconds * 0.5;
    
    
    cout << "Welcome to the game Calorie Burner!";
    cin.get();
    
    cout << "How many seconds would you like to run on the treadmill for? \n\n";
    cin >> seconds;
    
    cout << "\nYou will run for "  << seconds << " seconds.";
    cin.get();
    
    cout << burned;
    

    
    system("pause");
    return 0;
}
Last edited on Jun 22, 2013 at 5:03pm
Jun 22, 2013 at 12:37pm
cin >> digits;

remove the endl;
Jun 22, 2013 at 12:48pm
@ Tertius Kgatla

Oh my gawd I feel like an idiot xP thanks a lot.

Can anyone explain to me why "endl" works with cout but not cin? Why should it change?
Last edited on Jun 22, 2013 at 12:48pm
Jun 22, 2013 at 12:52pm
std::endl is a manipulator for output streams (only).

Input streams can't be flushed. Nor can you write (a new line) to an input stream.
Jun 22, 2013 at 12:54pm
Alright, I understand now. It's weird how I didn't notice that before.

Jun 22, 2013 at 12:57pm
This also means, if I want to make a new line after a cin, I have to use

 
cout << "\ntext ";


right?
Last edited on Jun 22, 2013 at 12:57pm
Jun 22, 2013 at 1:05pm
right indeed!!
Jun 22, 2013 at 5:01pm
Thanks for helping me with the cin, but I have another issue.

Sorry for posting so much, I just don't know what's going on...!
Jun 22, 2013 at 5:18pm
define or assign burned after you get seconds from user. coz if you define burned as seconds*5; compiler will assign a random value to seconds(like 15468)
Jun 22, 2013 at 5:21pm
How would I define it in the code?

Isn't that this part?

 
cout << burned;


Because the user enters the amount of seconds, so it's defined then...right...?

Last edited on Jun 22, 2013 at 5:26pm
Jun 22, 2013 at 5:30pm
Because the user enters the amount of seconds, so it's defined then...right...?


The problem is that you do the calculation for burned before seconds has a meaningful value.
Jun 22, 2013 at 5:35pm
So to fix the issue, I should move this part of the code

double burned= seconds * 0.5;

below this part
cin >> seconds;


Jun 22, 2013 at 5:45pm
Thats right. When you initialize seconds, C++ wont allow it to have a value of NULL, so it gives it a random value(whatever junk value is around when you initialize) in your case seconds gets a value of 30299. Then you assign

double burned = seconds(30299) *0.5
So then burned becomes 15149.5

if you were to re-arrange your code like so:

1
2
3
unsigned short int seconds;
cin >> seconds;
double burned= seconds * 0.5;


This would work perfectly

So yes,
double burned= seconds * 0.5;
should go under cin >> seconds
Last edited on Jun 22, 2013 at 5:47pm
Jun 22, 2013 at 11:29pm
closed account (EwCjE3v7)
You are using doubles try int
Topic archived. No new replies allowed.