0

For this program I am trying to make first ask for the users name, print this as a lowercase, and then make a menu, and then for option 1 the program asks the user to guess a random number between 1-100. When I try to run the below code, the error I get is:

program.cpp:51:11: error: 'name' was not declared in this scope; did you mean 'tzname'? 51 | cin>>name; | ^~~~ | tzname

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "splashkit.h"
#include <string>

using namespace std;


void play_game()
{
int random = rand() % 101; //has to be 101 as when it divides it will be between 1-100
std::cout << random << std::endl;
std::cout << "Guess a number: ";
while(true)
{
    int guess;
    std::cin >> guess;
    if(guess == random)
    {
        std::cout << "You win!\n";
        break; //stops program if guessed right, otherwise keep going
    }   
    else if (guess < random)
    {
        std::cout << "Too low\n";
    }   
    else if (guess > random)
    {
        std::cout << "Too high\n";
    }
    }
}

void toSmall(char * name)  //function to convert characters to lower case
{
int i=0;
while(name[i]!= '\0'){
    if(name[i]>=65 && name[i]<=90){
        name[i]= name[i]+ 32;
    }
    i++;
    }
}

 int main()
 {
    srand(time(NULL)); //to stop same sequence appearing everytime, seeds a random number each        
 time

 cout<<"\nEnter your name: ";
 cin>>name;
 toSmall(name) ; //converts all character to lowercase
 cout<<"Welcome To the Game:  \n"<<name;


int choice;
do
{
    std::cout << "0. Quit" << std::endl << "1. Play Game\n";
    std::cin >> choice;

    switch(choice)
    {
    case 0:
        std::cout << "Game quit\n";
        return 0;
    
    case 1:
        play_game();
        break;
    }
}
while(choice != 0);

}
Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • It seems fairly clear. In function `main` you wrote `cin>>name` but you have no defined variable called `name`. Presumably you want something like `string name` before the line in question. – jkb Aug 11 '22 at 05:15
  • You should have a `std::string name;` in main. (use std::string instead of char*, https://www.learncpp.com/cpp-tutorial/introduction-to-stdstring/). For changing to lowercase there is either std::to_lower or have a look here https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case – Pepijn Kramer Aug 11 '22 at 05:15
  • Like the snippet in your play_game():`int guess; std::cin >> guess` . You need to declare a type of `name`. PS: If the reply is helpful, you could click '✔' to mark it as the accepted answer. – Minxin Yu - MSFT Aug 11 '22 at 05:27
  • O.T.: `int random = rand() % 101; //has to be 101 as when it divides it will be between 1-100` The outcome will be in the range [**0**, 100]. According to [std::rand()](https://en.cppreference.com/w/cpp/numeric/random/rand), the function returns values _between ​0​ and RAND_MAX_ . Additionally, using modulo (`%`) with the outcome of `rand()` makes it less random. The link above provides sample code for a more reliable way to force the random numbers into a certain interval. In general, it's recommended not to use `rand()` anymore. (It's old and there are better alternatives now.) – Scheff's Cat Aug 11 '22 at 05:59

1 Answers1

1

What is wrong with my code? I get an error saying I have no declared 'name'

The problem is that there is no variable named name inside main(). That is, you're trying to read into name using cin>>name; when there is no variable called name that can be used here.

To solve this you can create a variable called name of type say std::string before trying to read into it:

std::string name;
std::cin >> name;

Note also that you can then pass name to toSmall by changing its declaration to

//-----------------------v------->pass by reference
void toSmall(std::string &pname)
{
   //code here
}

You can also use std::to_lower if you're allowed to use library function instead of writing one yourself.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Hi thank you! You fixed the problem, I tried doing what you said: void toSmall(std::string &name) but the name is remaining in capitals. What have I missed? – user7171717 Aug 11 '22 at 05:34
  • @user7171717 How are you iterating through the characters of the passed string? You can use [this demo](https://onlinegdb.com/MitsgbB4R). Note in the above given demo, i have added some comments so that you can fill out the remaining code there. – Jason Aug 11 '22 at 05:41
  • @user7171717 I cannot reproduce your second problem. toSmall worked well. – Minxin Yu - MSFT Aug 11 '22 at 05:48
  • I'm guessing that @user7171717 missed the very important character `&` in the definition of `toSmall`. – john Aug 11 '22 at 07:12