开发者

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 wi开发者_C百科ll 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.


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


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;
}


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$


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?


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");**


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜