|
-
December 14th, 2006, 12:34 AM
#1
weird problem
Hey, Im trying to make a program to help me study vocab. Its mostly just to learn c++, but im having some problems with the following code.
for(int b=0;b<numWords;b++)
{
system("cls");
randomNum = rand()%numWords;
cout<<word[randomNum]<<endl;
getline(cin, guess);
if(guess == definition[randomNum])
cout<<"Correct"<<endl;
else
{
cout<<"Incorrect"<<endl;
cout<<definition[randomNum];
}
for(int x=0;x<200000000;x++){}//pause
}
Ok, so the program inputs the vocab words and their defnitions. It will go through the loop as many times as there are words. It picks a random number, and displays that word. I guess, then it tells me if its correct or not, and if not displays the correct answer.
Problem is, the last cout, if i get the answer wrong, isnt working. For example if the word is evil, and i type nice (mean being correct), it will output incorrect, then pause, but the correct answer will not come up until later. if the next word is cow, it will display meancow (mean was the answer to evil). I have no clue if this will make sense to anyone, but if you have any ideas it would be appreciated. Thanks. Oh and I do have an ignore statement higher in the program, so thats not causing the problem.
-
December 14th, 2006, 01:09 AM
#2
Re: weird problem
Show your Full Code. this code Doesn't contain Enough information to answer your Question Like
what is the type of definition here wat is randomNum etc
thanx
-
December 14th, 2006, 01:33 AM
#3
Re: weird problem
How about;
Code:
cout<<definition[randomNum] << endl;
I'd also consider changing this bit of code
Code:
for(int x=0;x<200000000;x++){}//pause
Maybe this wait function would be better http://www.cplusplus.com/ref/ctime/clock.html
-
December 14th, 2006, 01:42 AM
#4
Re: weird problem
You need to post all of your code.
There is no way we can figure out what is wrong from this little snippet.
And use [ code ][ /code ] tags when posting source code.
Please rate my post if you felt it was helpful
-
December 14th, 2006, 01:57 AM
#5
Re: weird problem
 Originally Posted by laasunde
How about;
Code:
cout<<definition[randomNum] << endl;
I'd also consider changing this bit of code
Code:
for(int x=0;x<200000000;x++){}//pause
Maybe this wait function would be better http://www.cplusplus.com/ref/ctime/clock.html
Dude Are you aware What are you taking
?

Thanx
-
December 14th, 2006, 04:09 AM
#6
Re: weird problem
 Originally Posted by humptydumpty
Dude Are you aware What are you taking
?
Thanx
I can't understand you.
What was wrong with laasunde's suggestions?
Please rate my post if you felt it was helpful
-
December 14th, 2006, 04:47 AM
#7
Re: weird problem
 Originally Posted by humptydumpty
Dude Are you aware What are you taking
?
Thanx
Care to explain ?
-
December 14th, 2006, 05:14 AM
#8
Re: weird problem
 Originally Posted by cdjr84
I can't understand you.
What was wrong with laasunde's suggestions?
It is almost as evil as OP's solution to wait...
A while() loop eating 100% of the CPU is a bad idea, especially for laptop computers and multitasking OSes.
Using a non-portable waiting construct would be much better, since this code is probably not intended to be ported, and even if it were, having to change pieces of code is very usual when porting code.
Assuming this code runs under Win32, the OP can:
- Use the Win32 Sleep() function.
- Use getch()... It doesn't do the same thing, but I'm pretty sure it would be much more appropriate then waiting!
- Use system("PAUSE") ... It's ugly and I don't like it, but it is consistent with the system("cls") that the OP already uses.
- Use std::cin.ignore(std::numeric_limits<int>::max(), '\n') to ignore an input line... an empty line can be entered by the user.
- Finally, he can remove this system("cls") statement and forget about waiting...
Code would be portable, and far more convenient for the user having a good console, with a text history.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|