samuraiseoul
samuraiseoul

Reputation: 2967

Program stops after cin

Hi there I am writing this program but I can't get it to even start to see if the other code has problems.

I have this:

    int main()
{
    int answer;
    int test;
    cout << "Please Enter the number to be tested: ";
    cin >> test; //Gets number to be tested
        cout << "here";
    answer = factor(test);
    cout << "The answer is:" << answer;
    return 0;
}

now then. It will print out the first cout, and then it gets to the cin, takes the number but then won't do anything. Won't even print the second cout. Any ideas?

I"m pretty new and haven't really done much so any extra treating me like an idiot explanations are welcomed. :D Thanks.

Upvotes: 0

Views: 6528

Answers (5)

Malik
Malik

Reputation: 129

This is how it work "here " your screen doesnot be static by putting system("pause") you can do it ,on the other hand i have just made the function defination dummi still happen anything check there]


#include<iostream>
using namespace std;
int factor(int x)
{
    return x;
}
int main()
{
    int answer;
    int test;
    cout << "Please Enter the number to be tested: ";
    cin >> test; //Gets number to be tested
    cout << "here";
    answer = factor(test);
    cout << "The answer is:" << answer;

    **system("pause");**


}

Upvotes: -1

Tony
Tony

Reputation: 1

I got the same results when I was hitting enter on the keypad right after my input. If I hit return then the program runs fine. I thought enter and return are the same thing?

Upvotes: 0

user513112
user513112

Reputation: 1

Looks like the program is waiting for the input at the terminal . Once you provide the input and then press "Enter" it will automatically consider the input and the next cout statement works fine ... Check the below code segment ... ( nothing modified except the dummy implementation for factor, which is not the topic of discussion here )

enter code here

include

using namespace std;

int factor(int t) { return t; }

int main()

{ int answer; int test; cout << "Please Enter the number to be tested: "; cin >> test; //Gets number to be tested cout << "here"; answer = factor(test); cout << "The answer is:" << answer; return 0; }

O/p is : $ ./a.out Please Enter the number to be tested: 1234 hereThe answer is:1234user@ubuntu:~$ ./a.out Please Enter the number to be tested: 1234 hereThe answer is:1234$

Upvotes: 0

Christian
Christian

Reputation: 4249

Maybe there is something wrong with the factor function? An infinite loop? Then cout << "here" << endl; (to flush the output) should at least print "here".

Upvotes: 4

BastiBen
BastiBen

Reputation: 19860

I guess << endl;is missing in your cout lines. That causes the output buffer not not be flushed and nothing to appear on the screen. Although that might be dependent on the platform you are running it on. It might work on some systems that flush the output buffer permanently.

int main()
{
int answer;
int test;
cout << "Please Enter the number to be tested: ";
cin >> test; //Gets number to be tested
    cout << "here" << endl;
answer = factor(test);
cout << "The answer is:" << answer << endl;
return 0;
}

Upvotes: 1

Related Questions