Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code:
C++
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <unistd.h>

char* name;


using namespace std;

int main() {

    cout << "Hail adventurer!" << endl;
    sleep(1.5);
    cout << "What is your name?: ";
    cin >> name;
    cout << name << " ay?";
            
    
    return 0;
 }

I am brand new to C++ and tried to make a text-based adventure.. That didn't turn out as planned.. any help would be great. I know the problem is probably staring in the face.

What I have tried:

Google and Documentation.........
Posted
Updated 23-Oct-22 13:06pm

1 solution

The problem is you have a pointer that does not point to any actual data. Also name does not need to be a global variable. It can be within the scope of your main function.

You should probably use a string for this and you have already included its header so you could do this :
C++
int main()
{
    std::string name;

    cout << "Hail adventurer!" << endl;
    sleep(1.5);
    cout << "What is your name?: ";
    cin >> name;
    cout << name << " ay?";

    return 0;
 }
 
Share this answer
 
Comments
Brennon Nevels 23-Oct-22 19:09pm    
Thanks, I just started yesterday. Now everything makes a bit more sense

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900