char experiment = 'Q'

is what I got, however if I were to type in "AAA" or simply more letters than one then it
will bug out a bit for me.
Is there some kind of similar text to this but for char:

int mainval;
  while(true){
   cout<<"1. Start game";
   cout<<"2. Rules";
   cout<<"3. Quit game";
   cin>>mainval;
   if( cin.good() && mainval >=1 && mainval <=3 ) {
    break;
   }
   cin.clear();
   cin.ignore( INT_MAX, '\n' );
  }

Basicly if you type 1 or 2 it will continue, but anything else it will then reset and ask again.

Any idea how to do a similar thing, but for char?
(I only intend to do a simply Y/N question, (If possible, make it work with lower caps aswell ex: y/n)

Thank you.

You mean like converting a character into a usable input? Like pressing 'Q' to get "Quit game" and whatever? My knowledge of <stdio.h> is better than my knowledge of <iostream>, so I'm sorry if it seems too oldschool.

char input = getchar();
if(tolower(input) == 'q')
{
    //Code to quit game
}
if(tolower(input) == 'r')
{
    //Code to print the rules
}

And et. cetera.

Ys, but what happens if you were to put in a 1? Or AAAA or 1251 (Basicly, NOT what should be put in)

Then it resets?

Im sorry if this have confused you, but what I meant is that I used that example for my main menu, however in the game its about dices and I want it so when they are asked to throw a dice you can chooce: Y/N, I got an else to fix it if y ou type in something else.
However I thought a similar one to my main would help me, expecilly as if I type in YY or NN or AQ (More than ONE wrong letter) it bugs up and goes into the other ones turn.

I simply want a way too:
Check if the inputed char is either of Y N y n else it will reset the char and reask the question.
This can be achieved with a while, however I need the proper programming words for it.
In int it is:

if( cin.good() && mainval >=1 && mainval <=3 ) {
break; (If the int is an int and the choice is 1 or 3 or inbetwen then it breaks the while and it continues to next, else it resets)
}
cin.clear();
cin.ignore( INT_MAX, '\n' );
However here is the words for resetting.
However the INT_MAX should be changed, to CHAR_MAX ??

char experiment = 'Q'

is what I got, however if I were to type in "AAA" or simply more letters than one then it

experiment in your example above is a single char. Is it any wonder that trying to put more than one char into it causes problems?

There are many ways to deal with the user entering more than one character. For example, use proper C++ strings and just read the first character:

std::string userInput;

while(1) // loop forever until user enters sensible input
{
  cin >> userInput;
  if(tolower(userInput[0]) == 'y')
  {
   // code here
   break; // leave infinite loop
  }
  if(tolower(userInput[0]) == 'n')
  {
   // code here
   break; // leave infinite loop
  }
}

I've decided to skip this part guys after talking with my teacher.
Unfortunly, as I wanted to add something else, "special rules/goals" I've come upon a problem.
Basicly:

int dice1(void){
    int max = 6, min = 1;
    return (rand()%(max-min+1)+min);

I wanna turn into

int dice1(void){
    int max = maxpoints, min = minpoints;
    return (rand()%(max-min+1)+min);

The reason is so that I can have the maximum and minimum on the dice changeable.
Unfortunly it complains on:
`minpoints' undeclared (first use this function)
`maxpoints' undeclared (first use this function)
However I have declared that int minpoints = 1, maxpoints = 6 in a int earlier.

I am confused.

The only variables that exist inside the function are:

- variables you passed into the function (none in this case, because you pass in nothing)
- variables made inside it (which is max, and min)
- global variables that exist everywhere

Even though you may have created variables called minpoints and maxpoints earlier, they do not exist in the scope of this function.

commented: beat me to it. +4

1. You're not using a char variable, you're using an integer variable. Integers are strictly numerical.

2. Character variables can only hold 1 character. This is due to size limitation of the variable. You will need an array characters to accept input with more than one character.

If this sounds a bit over your head then don't worry about it, just ignore this part for now, your class should cover all that stuff.

The reason is so that I can have the maximum and minimum on the dice changeable.
Unfortunly it complains on:
`minpoints' undeclared (first use this function)
`maxpoints' undeclared (first use this function)
However I have declared that int minpoints = 1, maxpoints = 6 in a int earlier.

Variables are function specific, meaning that unless you:
1.Used the old variables as parameters for the function
or
2.Declared the variables globally. (not the best option)

Your variables will not exist in this new function. Have you learned about function parameters yet?

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int Add(int x, int y);

int main()
{
 int x,y;
 cout << "Enter value one" << endl;
 cin >> x;
 cout << "Enter value two" << endl;
 cin >>y;
 int result = Add(x,y); // Passing the variables as parameters.
 cout << "Their sum is: " << result;
 return 0;
}

int Add(int x, int y)
{
 int sum = x + y;
 return sum;
}

Have you seen an example to this effect? I'm sure your textbook has one. Read up on the chapter on Functions.

I managed to fix the problems.
Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.