i am trying to review what C++ stuff i have learned last year before high school starts, but the code i am writing does not run properly after i compile it.

the code compiles
the code does not execute. the code does not execute after Recruit() runs C_Name() to get a string.

i have removed all my badly spelled cout text for you)

// first text based RPG
// Test

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

int Start();
void Wait();
string Recruit();
string AskText(string prompt);

string C_Name();

int main()
{
	Start();

	return 0;
}

string C_Name()       //i think the problem is here
{
	
	string name ="l";
	int characters;

	characters = name.size();

	while(characters != 0)
	{
		if(characters <= 1)
		{
			cin>>name;
		}
	
			if(characters > 1)
			{
				return name;
			}
		else
		{
			characters = -1;
		}
	}

}

string AskText(string prompt)
{
	string text;

	cout<<prompt;
	cin>>text;

	return text;
}
/**this does not work but i will solve by myself later
*void Wait()
*{
*	char responce='a';
*
*	while((responce != 'y') || (responce != 'Y'))
*	{
*		cout<<endl;
*		cout<<"do you wish to continue? (y/n)";
*			cin>>responce;
*		cout<<endl;
*	}
*}
*/

string Recruit()
{
	char answer = 'n';
	string Name;

	cout<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl;
	
	Name = C_Name();
	while((answer !='y') || (answer != 'Y'))
        {
		cout<<"- "<<Name<<"? (y/n)"<<endl;
		cin>>answer;

		if(answer == 'y' || answer == 'Y')
		{
			return Name;
		}

		else
		{
			cout<<"-"<<endl;
			Name = C_Name();
		}
         }
}

int Start()
{
	cout<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl
		<<"-"<<endl;

	Recruit();

	return 0;
}

Recommended Answers

All 8 Replies

Your post is rather confusing, you'll have to answer these if you want any help:
What exactly do you think is the problem?
Note: I saw your comment indicating you think it's in C_Name(), but you'll have to elaborate.

Does it compile? If not, what does the compiler tell you?
It looks like it will compile, but I haven't tested it.

Does it execute? If so, does the execution vary from what you expect it to do? And if so, how does it vary from what you expect?
Again, I haven't tested it because I wouldn't know "normal" execution from "abnormal" execution, I don't know your goals.

Your post is rather confusing, you'll have to answer these if you want any help:
What exactly do you think is the problem?
Note: I saw your comment indicating you think it's in C_Name(), but you'll have to elaborate.

Does it compile? If not, what does the compiler tell you?
It looks like it will compile, but I haven't tested it.

Does it execute? If so, does the execution vary from what you expect it to do? And if so, how does it vary from what you expect?

the code compiles
the code does not execute. the code does not execute after Recruit() runs C_Name() to get a string.

i just edited this in my first post
*so far i just want the code to run through all the couts, take a user inputed name and store it for later use in a separate function and end the execution

Please explain your algorithm for C_Name(). I think you may have missed a few commands and/or built your if structures incorrectly.

string C_Name() 
{
 
	string name ="l"; // a default name for the character
	int characters;   //and integer for the .size() on the next line
 
	characters = name.size();   //this take the sting name and makes the letter length into a single number (ex: dad = 3 letters)
 
	while(characters != 0) //makes a loop to make sure your person has a name
	{
		if(characters <= 1) //says if your name has 1 letter in it you have to make a new name
		{
			cin>>name;
		}
 
			if(characters > 1) if your name has more than one letter return the value of name
			{
				return name;
			}
		else  //if there is something that does not fit in either if statements above it gives the int characters a -1 so it has to rerun the loop.
		{
			characters = -1;
		}
	}
 
}

Where do you get the size of the name that the user has entered? From what I see, the value of "characters" never changes once you enter the loop.

I think you need to take another look at the structure of your loop. You should take another look at your formatting too. Based on your formatting, I don't think your if statements work the way you think they do.

Properly formatted version of your code (no functional modifications):

string C_Name() 
{
 
  string name ="l"; // a default name for the character
  int characters;   //and integer for the .size() on the next line
 
  characters = name.size();   //this take the sting name and makes the letter length into a single number (ex: dad = 3 letters)
 
  while(characters != 0) //makes a loop to make sure your person has a name
  {
    if(characters <= 1) //says if your name has 1 letter in it you have to make a new name
    {
      cin>>name;
    }

    if(characters > 1) if your name has more than one letter return the value of name
    {
      return name;
    }
    else  //if there is something that does not fit in either if statements above it gives the int characters a -1 so it has to rerun the loop.
    {
      characters = -1;
    }
  }
 
}

now it is good, i just had to move my characters = name.size(); into the loop. thanks for the help

Hmmm.... are you sure about that?

It may be a semantic misunderstanding on my part but it really should be called twice, not once. The first time should be before the loop to properly initialize characters, like you already have/had. The second time should be after your input, to verify the length of the name.

i probably should before i keep going.

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.