Console application

Posts 1–13 of 13 · Page 1 of 1
Console application
Hello, I started learning C++ in school, wasn't really serious about it, but now i'm starting to like it. Anywayyy, I'm only trying some stuff on console application, but I have one question, how to make a loop, ex. I'm now making a game where you have to guess a number, and if you are correct, the application closes, if you are not, you have to start again, so yeah, how.?
Quote Originally Posted by rLes View Post
Hello, I started learning C++ in school, wasn't really serious about it, but now i'm starting to like it. Anywayyy, I'm only trying some stuff on console application, but I have one question, how to make a loop, ex. I'm now making a game where you have to guess a number, and if you are correct, the application closes, if you are not, you have to start again, so yeah, how.?
Use an infinite for loop and a break. This is in C. printf is the C++ equivalent of cout and scanf is the equivalent of cin.

Code:
int main()
{

    int guessNum, randNum; 

    for ( ; ; )
    {
        
        randNum = genRandNum(); //genRandNum is a function I made for generating a random number
        
        printf( "Enter your guess.\n\n" );
        scanf( "%d", &guessNum);

        if (guessNum == randNum)
        {

            printf( "Woohoo. You guessed correctly!" );

            Sleep(2000);

            break;

       } //end if

       else
       {

            printf( "The number you entered is not correct. Please try again.\n\n" );

            Sleep(2000);

            system("CLS"); //this clears the console screen

       } //end else

    } //end for

    return 0;

} //end main
Code:
\c++\1\main.cpp||In function 'int main()':|
\c++\1\main.cpp|17|error: 'Sleep' was not declared in this scope|
\c++\1\main.cpp|18|error: break statement not within loop or switch|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
what did i do wrong?
Quote Originally Posted by rLes View Post
Code:
\c++\1\main.cpp||In function 'int main()':|
\c++\1\main.cpp|17|error: 'Sleep' was not declared in this scope|
\c++\1\main.cpp|18|error: break statement not within loop or switch|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
what did i do wrong?
Post your code. You need to include the windows header at the top of your source file in order to use the Sleep function.

Code:
#include <windows.h>
Quote Originally Posted by Woodhouse View Post

Post your code. You need to include the windows header at the top of your source file in order to use the Sleep function.

Code:
#include <windows.h>

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

using namespace std; 

int main()
{
    printf("Guess the number from 0 to 15");
    int x;
    int guess;
    x = rand() % 15 + 1;
    cin >> guess;
    if (guess == x) printf("Correct");
        else if (guess < x) printf("Your guess is too low");
            else if (guess > x) { printf("Your guess is too high");
                                    Sleep(1500);
                                    break;
                                   }
    return 0;
}
Quote Originally Posted by rLes View Post
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

using namespace std; 

int main()
{
    printf("Guess the number from 0 to 15");
    int x;
    int guess;
    x = rand() % 15 + 1;
    cin >> guess;
    if (guess == x) printf("Correct");
        else if (guess < x) printf("Your guess is too low");
            else if (guess > x) { printf("Your guess is too high");
                                    Sleep(1500);
                                    break;
                                   }
    return 0;
}
I take it you haven't learned for loops yet? If you look in my code, that's what I'm using and how you can accomplish what you want to do. Also, the windows header should always be your first #include directive. I edited my code so it's a full console application instead of just showing the for loop. Another thing, the way you're generating numbers isn't truly random. You need to seed it. If you don't, you'll generate the same "random" numbers every time you run the program.

EDIT: Oh crap. Don't use printf/scanf, dude. Like I said, I'm using C, not C++. Continue to use cout/cin. The languages are similar, but have subtle differences.



Didnt get it, could you please make my code work, i want to see an example.
Quote Originally Posted by rLes View Post
Didnt get it, could you please make my code work, i want to see an example.
You need to learn how to use a for loop. I suppose you could use a while loop as well. Also, like I said, the "random numbers" will be the same every time you run it. Watch the two videos I posted. I don't use C++. My code I originally posted was C. Which is why it didn't run when you tried compiling it. This should work.

Code:
#include <windows.h>
#include <iostream>

using namespace std;

int main()
{

    for ( ; ; )
    {

        cout << "Guess a number from 0 to 15" << endl;

        int guess;

        int x = rand() % 15 + 1;

        cin >> guess;

        if (guess == x)
        {

            cout << "Your guess was correct!" << endl;
            Sleep(2000);
            break;

        } //end if

        else if (guess < x)
        {

            cout << "The number " << x << endl;
            cout << "Your guess was too low!" << endl;
            Sleep(2000);
            system("ClS");

        } //end if

        else
        {

            cout << "The number was " << x << endl;
            cout << "Your guess was too high" << endl;
            Sleep(2000);
            system("CLS");

        } //end else

    } //end for

    return 0;

} //end main
Errors.
Code:
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\windef.h||In function 'int main()':|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\windef.h|8|error: expected unqualified-id before string constant|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\winnt.h|2444|error: expected '}' before end of line|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\winnt.h|2444|error: expected declaration before end of line|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
Quote Originally Posted by rLes View Post
Errors.
Code:
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\windef.h||In function 'int main()':|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\windef.h|8|error: expected unqualified-id before string constant|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\winnt.h|2444|error: expected '}' before end of line|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\winnt.h|2444|error: expected declaration before end of line|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
There shouldn't be any errors. Did you copy and paste the code exactly as it's shown? It's working fine for me.

I'm sure, tripple checked...
Seems like your MingW installation has some configuration issues. Using a fresh build should do the trick although the need for Windows API is totally unnecessary.

That being said that is some ugly code. This is better:
Code:
#include <cstdlib>
#include <iostream>
#include <limits>
#include <ctime>

int main() {
  std::srand(std::time(0));

  int x = std::rand() % 16;
  int guess;

  while ( (std::cout << "Guess the number between 0 and 15: ") &&
          (!(std::cin >> guess) || (guess != x)) ) {
    std::cout << "That's not right!" << std::endl;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }

  std::cout << "That's right! The number is " << x << '.';
}
Quote Originally Posted by Fovea View Post
Seems like your MingW installation has some configuration issues. Using a fresh build should do the trick although the need for Windows API is totally unnecessary.

That being said that is some ugly code. This is better:
Code:
#include <cstdlib>
#include <iostream>
#include <limits>
#include <ctime>

int main() {
  std::srand(std::time(0));

  int x = std::rand() % 16;
  int guess;

  while ( (std::cout << "Guess the number between 0 and 15: ") &&
          (!(std::cin >> guess) || (guess != x)) ) {
    std::cout << "That's not right!" << std::endl;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }

  std::cout << "That's right! The number is " << x << '.';
}
I guess he clicked on an error and itbrought up some random header file. He never switched back to his main source file which led him to putting his code in the header. He pasted the code into the header which led to the errors. And yes, much more efficient than what I had originally. I don't use C++ so I have no idea what some of those lines do. Though he hasn't even learned control statements in his class...
Posts 1–13 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us