Looping and text function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{

//Value of total experience
int total;
cout << "Enter the your total Experience value:";
cin >> total;

//Value of current experience
int current;
cout << "Enter the current Experience value:";
cin >> current;

//Value of monster's experience
int monster;
cout << "Enter the value of the Monster's Experience:";
cin >> monster;

//Difference between total and current experience
int factor;
factor = total - current;

//Value of number of monsters to be killed
int number;
number = factor / monster;

int numberofmobs;
cout << "Number of monsters to kill:";
cout << number << endl;

  // wait until user is ready before terminating program
  // to allow the user to see the program results
  system("PAUSE");
  return 0;
}


All of these are in console application.

I'm currently using c++ for dummies 6th edition.
I was wondering how do i make a loop at the end of the function, so that i wont have any hassle executing it again...and again..

Entering a text output to execute a function from 3 different functions to choose from, at the beginning of the console. Is it possible?

E.g
Enter "1" to run "Monsters to be killed"
Enter "2" to run "etc"
Enter "3" to run "etc"

Thanks mates for your kind attention :)

Last edited on
1) That is what a 'main loop' is for. Video games mostly use these. But the answer isn't putting it at the end, it is putting it at the beginning and testing it against a boolean value until the user wants to exit the application.

Sample Syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <conio.h>
#include <Windows.h>

int main()
{
    bool endApp; // variable to test against ending the application
    
    while (endApp != true)
    {
         char inputChar;

        //application code here
        
        cout << "Press Q to quit the application" << endl;
        inputChar = getche();
        
        if (inputChar == 'q' )
              endApp == true; // Application will then terminate, else it will start over

    }
    return 0;
}


There are (of course) more complex and somewhat better ways to do this, but this is a start (and something I came up with on the spot ^_^).

2) Of course! This is programming! There are algorithms for doing everything! If you are learning from the book, it will teach you about user-defined functions. If you plan on being serious about programming, this will be an IMPORTANT chapter. Make sure you pay close attention.

Also, the book will not tell you this, but system("PAUSE") is good for simple applications like yours, but if you do this professionally, it is frowned upon because it is OS-specific and programming is about portability! Try using

1
2
cin.ignore();
return 0;


It does the same exact thing as system("PAUSE");

Hope I have helped you on your C++ learning quest!
Hmm i think im having some commands overflow, how do i specifically restrict the input value to numeral input only?

Because in the event of inputting the values, on the press of a alphabet, it will jam the whole function causing it to display all of its text.

Sry and thanks again
Last edited on
That you will learn when you get into error handling, but it is simple logic. You want to put it after the input is received from the user. Also, it needs to be sent back through the loop if the process fails.

One way is to use the iostream's .good() function, which determines if the input was valid. If it returns 1, then it is the same as saying it is valid. If 0 then it is false

Pseudocode:
int inputVal;

if inputVal != a number
print errorMessage()

The source code may look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <conio.h>
#include <Windows.h>

using namespace std;

int main()
{
     int inputValue;
     bool check; // this is just a check to break the while loop when we want to finish

 
    while( check != true)
    {
          cout << "Please input a value" << endl;
          cin >> inputValue; 

         if (cin.good() == 1)
         {
               // put your normal working operations here
              check = true;
         }
         else if (cin.good() == 0)
         {
               cout << "You did not enter a valid integer. Press any button to try again." << endl;
               getche();
         }
     }

     return 0;
}


Of course, this is just a skeleton of what needs to be done, but hopefully this helps you understand the logic behind what you need to implement in your application. I hope this has helped.
Last edited on
Topic archived. No new replies allowed.