I have been trying to get a bug fixed. which is based on upon when a user inputs a number instead of a string as a name for the user. the program loops, i have been trying to implement Cin.reset and cin.ignore(numeric_limits::max(),’\n’);. But it does not work, i did this in a while loop, but all forsaken.
Down below is my code:
#include "stdafx.h"
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
string Yes;
int bet;
int color;
string name;
string answer;
//string choice;
int age;
char choice, colour;
int yes;
int no;
int konto = 1000;
int number;
int rand_number;
cout << "Hello and welcome to Roulette House" << endl;
cout << "What is Your name:" << endl;
cin >> name;
cout << "Your name is: \t" << name << endl;
while (!(cin >> name)) {
cin.reset();
cin.ignore(numeric_limits<streamsize>::max(), ’\n’);
cout << "please write only characters" << endl;
cin >> name;
}
cout << "Do you want to start the game?" << endl;''
cout << "please write yes or no?" << endl;
cin >> answer;
if (answer != "yes") {
cout << "ok thanks for stepping by" << "!!!!!" << endl;
return 0;
}
cout << "welcome" << endl;
cout << "This is the rules of the game: You must be 18 or over to play this game!" << endl;
cout << "As a bonus you will start the game with 1000kr" << endl;
cout << "you have" << konto << "kr in your account" << endl;
cout << "How old are you?";
cin >> age;
if (age >= 18)
{
cout << "you are allowed to play" << endl;
}
else
{
cout << "sorry your are to young to play" << endl;
return (0);
}
while (true)
{
cout << "Place your bet. Remember you can only bet 100kr,300kr and 500kr" << endl;
cin >> bet;
while (bet != 100 && bet != 300 && bet != 500) {
cout << "please choose between 100 or 300 or 500" << endl;
cin >> bet;
}
while (true)
{
cout << "You can choose one of the options, c (color) or n (number)?" << endl;
cin >> choice;
if (choice != 'c' && choice != 'C' && choice != 'n' && choice != 'N') {
break;
}
}
if (toupper(choice) == 'N') {
cout << "choose a number between 1 and 36" << endl;
cin >> number;
while (number < 1 || number > 36) {
cout << "Enter a number between 1 and 36" << endl;
cin >> number;
}
srand(time(0));
rand_number = rand() % 36 + 1;
cout << "the winning number is" << rand_number << "." << endl;
if (choice == 'N' && number != rand_number)
cout << "You lost $" << 1*bet << endl;
}
else {
cout << "You win!" << endl;
}
}
else if (choice == 'c' || choice == 'C')
{
colour = ' ';
while (colour != 'b' && colour != 'r')
{
cout << "color b (black) or r (red)" << endl;
cin >> colour;
if (colour == 'b') {
cout << "you choosed black" << endl;
}
else if (colour == 'r') {
cout << "you choosed red" << endl;
}
}
char colors[2] = { 'b', 'r' };
int rand_index = rand() % 2;
if (colors[rand_index] == colour) {
cout << "You won!" << endl;
}
else {
cout << "You lost!" << endl;
}
}
cout << "Bet again yes or no?" << endl;
cin >> answer;
if (answer != "yes") {
cout << "ok thanks for stepping by" << "!!!!!" << endl;
return 0;
}
}
return 0;
}
Thank you for youre time and i will appreciate all feedback given.
Aucun commentaire:
Enregistrer un commentaire