Prompt User to Loop - c++

I have created a Prompt User to Loop to restart the program however when I type anything including "y" it just goes prompts me with "Press any key to continue . . . " and then closes. Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
char again = 'Y';
while (again == 'y' || again == 'Y')
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout << "Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
{
cout << "Restart? (y/n) ";
cin >> again;
}
system("pause");
return 0;
}
}
Thank you.
This is my first project and I am completely new to coding so I am not sure what to do.

Your return 0 is misplaced, it should be outside the while loop.
And you shouldn't put a bracket {} around your cin >> again.
{ // this bracket doesn't needed
cout << "Restart? (y/n) ";
cin >> again;
}
Just use bracket when it needed e.g. in for, if, function, etc. It will not make a compile error. But I think it makes you misread and think the while loop was closed.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
char again = 'Y';
while (again == 'y' || again == 'Y')
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout << "Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
{ //This bracket doesn't need.
cout << "Restart? (y/n) ";
cin >> again;
continue;
} // This bracket doesn't need.
system("pause");
return 0;
}
}

Related

Repeat checker goes back to menu even after entering N to input

The goal is to create a five-in-one program for various math stuff. The individual blocks work. After selecting and using a block, the program should then send a prompt asking the user whether to go back to the menu or terminate the program, this also works.
However, the function which checks whether you've inputted "Y", "y", "N", or "n" does not work. Instead, it automatically returns you to the main menu even after inputting "N" or "n", which should terminate the program.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
// checks if prime is positive or nah
if (x <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < x; i++)
if (x % i == 0)
return false;
return true;
}
void Repeatcheck(char repeat){
//checks whether you inputted anything other than Y, y, N, or n
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
//Option selection
int OptSel;
//repeat selection
char repeat;
repeat = 'Y';
//Optsel 1
int prime, i, primed2=0, flag=0;
//Optsel 2
int j,rows;
//Optsel 3
int t1 = 0, t2 = 1, nextTerm = 0;
//Optsel 4
int factorialinput;
long factorial = 1.0;
do{
system("cls");
repeat = 'Y';
startoptsel:
std::cout << "\n----MATHS PROGRAM----" << std::endl;
std::cout << "\n wats poppin?" << std::endl;
std::cout << "1.) prime number checker" << std::endl;
std::cout << "2.) right triangle drawer" << std::endl;
std::cout << "3.) fibonacci" << std::endl;
std::cout << "4.) factorial" << std::endl;
std::cout << "5.) exit" << std::endl;
std::cin >> OptSel;
//check whether option seleccted is available.
if (!std::cin || OptSel <= 0 || OptSel > 5) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "\nThat's not an option, dumbass." << std::endl;
std::cout << "\nInput numbers from 1-5." << std::endl;
goto startoptsel;
}
if (OptSel == 1) {
system("cls");
primechecker:
std::cout << "\n----Prime number checker----" << std::endl;
std::cout << "Enter number: " << std::endl;
std::cin >> prime;
// auto filters everything as not prime below 1
primed2=prime/2;
for(i = 2; i <= primed2; i++){
if(prime % i == 0)
{
cout << "\n " << prime << " is not a prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "\n " << prime << " is a prime."<<endl;
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto primechecker;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 2) {
system("cls");
triangledrawer:
cout << "\n----RIGHT TRIANGLE DRAWER----" << std::endl;
cout << "\nInput number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++){
for(j=1;j<=i;j++)
cout<< ' ' << j;
cout<<endl;
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto triangledrawer;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 3){
system("cls");
fibonacciprinter:
cout << "\n----Fibonacci printer----";
cout << "\nEnter the number of terms: ";
cin >> prime;
cout << "Fibonacci Series: ";
for (int i = 1; i <= prime; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
if (!cin) {
system("cls");
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "please input a number" << std::endl;
goto fibonacciprinter;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
}
if (OptSel == 4){
system("cls");
cout << "Enter a positive integer: ";
cin >> factorialinput;
cout << "Factorial of ";
if (factorialinput < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= factorialinput; ++i) {
cout << i << " ";
factorial *= i;
}
cout << "= " << factorial;
}
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin>>repeat;
Repeatcheck(repeat);
repeat = repeat;
}
if (repeat == 'n' || repeat == 'N'){
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
}while (OptSel != 5);
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}
Rewriting the program to instead run when repeat = Y or y and removing the specific instance of terminating the program when N or n is entered instead results in the program terminating automatically no matter what is inputted when the Repeatcheck function is called.
#include <iostream>
#include<cctype>
#include<cstdlib>
#include <ctime>
#include <bits/stdc++.h>
using namespace std;
bool Primecheck(int x){
...
}
void Repeatcheck(char repeat){
while (repeat != 'Y' && repeat != 'y' && repeat != 'N' && repeat != 'n' ){
system("cls");
std::cout << "that's not an option, dumbass." << std::endl;
std::cout << "\n Return to main menu? Y/N" << std::endl;
cin >> repeat;
}
}
int main()
{
...
do{
...
}while (OptSel != 5 && repeat == 'Y' || repeat == 'y');
system("cls");
std::cout << "\nJob done? JOB DONE! Aight wrap it up lesgo." << std::endl;
return 0;
}

Error: expected primary-expression before ']' token. Need assitance with function definition and calling

This is my first semester of computer science, and I need help with my first project. So far, it's still a mess, and I am mainly doing test cases to ensure basic things like my functions work.
The goal of the project is to ask the user how many robots they want to make, name them, then they can use the robot's name (their unique identifier) to move the robots along an X-Y axis, which is really just the program adding or subtracting to a .Xvalue or .Yvalue.
My MenuFunction works, but I am having trouble with the MoveFunction. Basically, I want the MoveFunction to ask the user which robot they want to use, go through the RobotArray, and print the Robot's name once found.
Again, this is just a test case so I can better understand the coding. Right now, I am getting two errors:
main.cpp:42:33: error: expected primary-expression before ‘]’ token
42 | string MoveFunction(RobotArray[], robotName, NumberOfRobots);
main.cpp:62:16: error: no match for call to ‘(std::string {aka std::__cxx11::basic_string}) ()’
62 | MoveFunction();
I don't know what to do for the first error, but I think the latter is due to my not having any objects in the function call, but I wouldn't know what to put in there anyway.
My complete code is below:
#include <iostream>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
int main()
{
int NumberOfRobots;
string robotName;
cout << "Enter the number of robots" << endl;
cin >> NumberOfRobots;
cout << endl << "Enter their name(s)" << endl;
userRobot RobotArray[NumberOfRobots];
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> robotName;
RobotArray[i].name = robotName;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
string MoveFunction(RobotArray[], robotName, NumberOfRobots);
{
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name;
}
}
}
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
cout << "Which robot would you like to move?";
MoveFunction();
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
First off, userRobot RobotArray[NumberOfRobots]; is a variable-length array, which is a non-standard extension supported by only a few compilers. To create an array whose size is not known until runtime, you should use new[] instead, or better std::vector.
That said, your main issue is that you are trying to define your MoveFunction() function inside of your main() function, which is not allowed. But, even if it were, you are not declaring it correctly. It has an erroneous ; on it. And RobotArray, robotName, and NumberOfRobots are not types, but variables. Like a variable, a function parameter always starts with a type. There are no untyped parameters/variables in C++.
You need to either:
move MoveFunction() outside of main(), and fix its declaration to use proper types, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
void MoveFunction(userRobot RobotArray[], int NumberOfRobots)
{
cout << "Which robot would you like to move?";
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name << endl;
return;
}
}
cout "Robot not found" << endl;
}
int main()
{
cout << "Enter the number of robots" << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector<userRobot> RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl;
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> RobotArray[i].name;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
MoveFunction(RobotArray.data(), NumberOfRobots);
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
or change MoveFunction() into a lambda, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
int main()
{
cout << "Enter the number of robots" << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector<userRobot> RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl;
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> RobotArray[i].name;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
auto MoveFunction = [&]{
cout << "Which robot would you like to move?";
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name << endl;
return;
}
}
cout "Robot not found" << endl;
};
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
MoveFunction();
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
You have a semi-colon at the end of MoveFunction() definition, In C++ when you define a function the name of the function does not end with a semi-colon.

Hangman only gets first letter of secret word

I am trying to write a hangman program for an assignment. I've written code I thought would work, yet when testing it with the secret word, "IMPOSSIBLE", it only reads the "I" and nothing else. I tried changing all my strings to character lists but I don't think that was the issue. Does anyone have any advice on what I am doing incorrectly?
Thanks,
Keith.
Here is the code:
/* CSCI 261 Assignment 5: Hang Man Game
*
* Author: Keith Danielson
*
* A program that runs a simple hang man game
*/
// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <string>
// We will (most of the time) use the standard library namespace in our programs.
using namespace std;
//Defining the secret word as a constant
//const string SECRET_WORD = "IMPOSSIBLE";
int main() {
const char SECRET_WORD[10] = {'I','M','P','O','S','S','I','B','L','E'};
const int SECRET_WORD_LENGTH = 10;
//Defining the number of wrong guesses available, found letters, wrong guesses, and user choice.
int guesses = 7;
char foundLetters[SECRET_WORD_LENGTH];
char wrongGuesses[guesses];
char userChoice;
//Filling foundLetters with underslashes based on the length of the secret word.
for (int i = 0; i <= SECRET_WORD_LENGTH; i++) {
foundLetters[i] = '_';
}
cout << "Welcome to hangman!" << endl;
for (int i = 0; i <= 7; i++) {
if (guesses == 0){
break;
}
cout << "Take a guess: ";
for (int j = 0; j <= SECRET_WORD_LENGTH; j++) {
cout << foundLetters[j] << " ";
}
cout << "\n" << "Your guess: ";
cin >> userChoice;
//if the user input is lowercase it'll be made upper case.
if (islower(userChoice)) {
userChoice = toupper(userChoice);
}
for (int j = 0; j <= SECRET_WORD_LENGTH; j++) {
//if (userChoice == foundLetters[j]) {
// cout << "You already guessed" << userChoice << "." << endl;
// break;
//}
if (userChoice == SECRET_WORD[j]) {
cout << "There's a " << userChoice << "!" << endl;
foundLetters[j] = userChoice;
break;
}
else if (userChoice != SECRET_WORD[j]) {
guesses = guesses - 1;
cout << "Sorry. No " << userChoice << "'s." << endl;
wrongGuesses[i] = userChoice;
if (guesses == 0) {
cout << "You lose! Try again.";
break;
}
else {
cout << "You have " << guesses << " remaining." << endl;
break;
}
}
}
}
return 0; // signals the operating system that our program ended OK.
}
Try something more like this instead:
// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <string>
#include <limits>
// We will (most of the time) use the standard library namespace in our programs.
using namespace std;
//Defining the secret word as a constant
const char SECRET_WORD[10] = {'I','M','P','O','S','S','I','B','L','E'};
const int SECRET_WORD_LENGTH = 10;
//Defining the number of wrong guesses available
const int WRONG_GUESSES = 7;
bool hasLetter(const char *letters, int numLetters, char ch) {
for (int i = 0; i < numLetters; ++i) {
if (letters[i] == ch) {
return true;
}
}
return false;
}
int main() {
char foundLetters[SECRET_WORD_LENGTH];
int foundLetters = 0;
char wrongLetters[WRONG_GUESSES];
int wrongLettersLength = 0;
int wrongGuessesLeft = WRONG_GUESSES;
char userChoice;
int found;
//Filling foundLetters with underslashes based on the length of the secret word.
for (int j = 0; j < SECRET_WORD_LENGTH; ++j) {
foundLetters[j] = '_';
}
cout << "Welcome to hangman!" << endl;
while (true) {
cout << "Take a guess: ";
for (int j = 0; j < SECRET_WORD_LENGTH; ++j) {
cout << foundLetters[j] << " ";
}
cout << "\n";
cout << "Your guess: ";
cin >> userChoice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//if the user input is lowercase it'll be made upper case.
userChoice = toupper(userChoice);
if (hasLetter(foundLetters, SECRET_WORD_LENGTH, userChoice) ||
hasLetter(wrongGuesses, wrongGuessesLength, userChoice))
{
cout << "You already guessed " << userChoice << "." << endl;
continue;
}
found = 0;
for (int j = 0; j < SECRET_WORD_LENGTH; ++j) {
if (SECRET_WORD[j] == userChoice) {
foundLetters[j] = userChoice;
++found;
}
}
if (found > 0) {
cout << "There's " << found << " " << userChoice << (found > 1 ? "'s" : "") << "!" << endl;
foundLettersLength += found;
if (foundLettersLength == SECRET_WORD_LENGTH) {
cout << "You win!";
break;
}
}
else {
cout << "Sorry. No " << userChoice << "'s." << endl;
wrongLetters[wrongLettersLength++] = userChoice;
if (--wrongGuessesLeft == 0) {
cout << "You lose! Try again.";
break;
}
cout << "You have " << wrongGuessesLeft << " guess" << (wrongGuessesLeft > 1 ? "es" : "") << " remaining." << endl;
}
}
return 0; // signals the operating system that our program ended OK.
}
Alternatively:
// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <string>
#include <limits>
#include <set>
// We will (most of the time) use the standard library namespace in our programs.
using namespace std;
//Defining the secret word as a constant
const string SECRET_WORD = "IMPOSSIBLE";
//Defining the number of wrong guesses available
const int WRONG_GUESSES = 7;
int main() {
//Filling foundLetters with underslashes based on the length of the secret word.
string foundLetters(SECRET_WORD.size(), '_');
set<char> guessedLetters;
int wrongGuessesLeft = WRONG_GUESSES;
char userChoice;
int found;
cout << "Welcome to hangman!" << endl;
while (true) {
cout << "Take a guess: ";
for (int j = 0; j < foundLetters.size(); ++j) {
cout << foundLetters[j] << " ";
}
cout << "\n";
cout << "Your guess: ";
cin >> userChoice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//if the user input is lowercase it'll be made upper case.
userChoice = toupper(userChoice);
if (!guessedLetters.insert(userChoice).second)
{
cout << "You already guessed " << userChoice << "." << endl;
continue;
}
string::size_type pos = SECRET_WORD.find(userChoice);
if (pos != string::npos) {
found = 0;
do {
foundLetters[pos] = userChoice;
++found;
pos = SECRET_WORD.find(userChoice, pos+1);
}
while (pos != string::npos);
cout << "There's " << found << " " << userChoice << (found > 1 ? "'s" : "") << "!" << endl;
if (foundLetters == SECRET_WORD) {
cout << "You win!";
break;
}
}
else {
cout << "Sorry. No " << userChoice << "'s." << endl;
if (--wrongGuessesLeft == 0) {
cout << "You lose! Try again.";
break;
}
cout << "You have " << wrongGuessesLeft << " guess" << (wrongGuessesLeft > 1 ? "es" : "") << " remaining." << endl;
}
}
return 0; // signals the operating system that our program ended OK.
}

C++ Hangman Code won't work correctly

my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;
while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}
cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;
while (letter != 'N' && letter != 'n')
{
while (wrong_guesses < MAX_TRIES)
{
cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}
cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}
cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}
int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}
void manual()
{
string word;
cout << endl << "INPUT WORD: " << endl;
cin >> word;
return;
}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;
srand(time(0));
word = rand() % 20;
while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}
do
{
fin >> x;
roc[i++] = char(x);
} while (x);
return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;
for (i = 0; i<MAX_LENGTH; i++)
{
if (secretword[i] == 0)
{
break;
}
if (guess == guessword[i])
{
return 0;
}
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);
for (i = 0; i<length; i++)
{
unknown[i] = '*';
}
unknown[i] = 0;
}
Again
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.

C++ my program reads backspace as a character

I am working with a c++ program, but I am stuck with annoying bug. The bug is that when i type the password, it counts backspace as a character so can I fix it? Here is the code.
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
string password, username, lon, nu, np;
char c;
int StarNum = 0, humanproof;
cout << "Do you wanna create a user or login?";
cout << "\nLogin" << endl;
cout << "New" << endl;
cin >> lon;
if(lon=="login"){
goto login;
}
if(lon=="new"){
goto newa;
}
login:
cout << "Username: ";
cin >> username;
lol:
cout << "Password: ";
while (c != 13)
{
c = (char)getch();
if(c == 13) break;
StarNum++;
password += c;
cout << "*";
if (c == 127 || c == 8){
//go here to fix the problem
}
password = "";
goto lol;
}
}
if(username == "user" && password == "pass" || username == nu && password == np){
cout << "\nYou are logged in.";
goto options;
} else {
cout << "\nusername or password is wrong" << endl;
return 0;
}
return 0;
newa:
cout << "Username:";
cin >> nu;
cout << "password:";
cin >> np;
cout << "Type the number fourhoundred and twentie three too proof you are a human: ";
cin >> humanproof;
if(humanproof == 423){
cout << "The username is " << nu << endl;
for(int no = 0; no <= 100; no++){
cout << endl;
}
goto login;
return 0;
}
if(humanproof!=423){
cout << "wrong answer!";
return 0;
}
options:
int op;
cout << "\nwhat do you want to do?" << endl;
cout << "1. Calculator" << endl;
cout << "2. About"<< endl;
cout << "3. Just for fun" << endl;
cout << "4. Exit" << endl;
cin >> op;
if(op==1){
goto calculator;
}
if(op==2){
goto info;
}
if(op==3){
goto fun;
}
if(op==4){
return 0;
}
else{
cout << "you entered a invalid number. " << endl;
return 0;
}
calculator:
double n1, n2, sum;
int opa;
cout << "Choose a operation" << endl;
cout << "1. Addition/+" << endl;
cout << "2. Subscraction/-" << endl;
cout << "3. Multiplication/x" << endl;
cout << "4. Divsion/ /" << endl;
cin >> opa;
if(opa == 1){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 + n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 2){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 - n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 3){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 * n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 4){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 / n2;
cout << "the sum is " << sum;
return 0;
}
if(opa > 4){
cout << "You entered a invalid number";
goto calculator;
}
info:
cout << "Created by Bergur 2013";
return 0;
fun:
cout << "You want an eyepad(ipad)?";
}
Your code already checks:
while (c != 13)
And prevent newline from being handled, do similar to what you need with the backspace character, whose number is 8
To fix your issue, before:
StarNum++;
Add:
if (c == 8 && StarNum > 0) {
StarNum--;
if (password.size () > 0)
password.resize (password.size () - 1);
continue;
}
Please format your code. Also, try to provide a minimal code which reproduce the problem, not the whole code.
Unrelated to your problem
Try not to use goto.
Do not use ASCII values, but use the char literals instead, i.e. use '\n' instead of 13.
Also, you can simply add an iother condition inside your while:
while (c != '\n' && c != ' ')