CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: weird problem

  1. #1
    Join Date
    Dec 2006
    Posts
    3

    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.

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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

  3. #3
    Join Date
    Jan 2003
    Posts
    615

    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

  4. #4
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    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

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: weird problem

    Quote 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

  6. #6
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: weird problem

    Quote 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

  7. #7
    Join Date
    Jan 2003
    Posts
    615

    Re: weird problem

    Quote Originally Posted by humptydumpty
    Dude Are you aware What are you taking
    ?


    Thanx
    Care to explain ?

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: weird problem

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured