"Cin" Issue solved, but new one arose.

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
cin >> digits;

remove the endl;
@ 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
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.
Alright, I understand now. It's weird how I didn't notice that before.

This also means, if I want to make a new line after a cin, I have to use

 
cout << "\ntext ";


right?
Last edited on
right indeed!!
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...!
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)
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
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.
So to fix the issue, I should move this part of the code

double burned= seconds * 0.5;

below this part
cin >> seconds;


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
You are using doubles try int
Topic archived. No new replies allowed.