C++ Problem

Knight7770

Hippopotomonstrosesquiped aliophobia
Reaction score
187
I'm making a random name generator using C++ (I don't know too much about the language; I'm still learning), and sometimes, it generates a name that does not correspond to the numbers which make it generate. I will post the code now:
Code:
#include <iostream>
#include <string>
#include <conio.h>
#include <time.h>
using namespace std;
//==================
int main()
{
	/*Hidden names (thanks to these people for letting me use them):
	Rao Dao Zao
	Oninuva
	Huurka
	varsaigen
	FamousPker(49)
	NearbyHermit
	Ash
	MasterHaosis*/
	//Variables
	int numberofnames;
	int namepart1, namepart2, namepart3, namepart4, namepart5;
	const unsigned randnumber = 12;
	string namepart[5][randnumber];
	//Random Set-Up
	srand(time(NULL));
	//Set up namepart array:
	//Part 1:
	namepart[0][0] = "Ak";
	namepart[0][1] = "Ser";
	namepart[0][2] = "Hu";
	namepart[0][3] = "On";
	namepart[0][4] = "Va";
	namepart[0][5] = "Rao";
	namepart[0][6] = "Fa";
	namepart[0][7] = "Nea";
	namepart[0][8] = "A";
	//Part 2:
	namepart[1][0] = "tu";
	namepart[1][1] = "ur";
	namepart[1][2] = "'";
	namepart[1][3] = "in";
	namepart[1][4] = "rs";
	namepart[1][5] = " Dao";
	namepart[1][6] = "mo";
	namepart[1][7] = "rby";
	namepart[1][8] = "s";
	//Part 3:
	namepart[2][0] = "vu";
	namepart[2][1] = "ka";
	namepart[2][2] = "xe";
	namepart[2][3] = "u";
	namepart[2][4] = "ai";
	namepart[2][5] = " Zao";
	namepart[2][6] = "us";
	namepart[2][7] = "her";
	namepart[2][8] = "h";
	//Part 4:
	namepart[3][0] = "tch";
	namepart[3][1] = "pe";
	namepart[3][2] = "zae";
	namepart[3][3] = "va";
	namepart[3][4] = "ge";
	namepart[3][5] = "";
	namepart[3][6] = " Pk";
	namepart[3][7] = "mi";
	namepart[3][8] = "gu";
	//Part 5:
	namepart[4][0] = " Rex";
	namepart[4][1] = "";
	namepart[4][2] = "que";
	namepart[4][3] = " Wan";
	namepart[4][4] = "n";
	namepart[4][5] = "";
	namepart[4][6] = "er";
	namepart[4][7] = "t";
	namepart[4][8] = "y";
	//============================
	cout<<"************************************"<<endl;
	cout<<"************Welcome*****************"<<endl;
	cout<<"************to the******************"<<endl;
	cout<<"*******Random Name Generator********"<<endl;
	cout<<"************************************"<<endl;
	cout<<"~~~~~~~~~~~Version 1.00~~~~~~~~~~~~~"<<endl;
	cout<<"How many names would you like?"<<endl;
	cin>>numberofnames;
	if(numberofnames<=0)
	{
		cout<<"You have entered an invalid number.  Please restart the program."<<endl;
		_getch();
		return 0;
	}
	else
	{
		for(int a=0;a<numberofnames;a++)
		{
			//Name part 1:
			namepart1 = rand() % 10 + 1;
			namepart2 = rand() % 10 + 1;
			namepart3 = rand() % 10 + 1;
			namepart4 = rand() % 10 + 1;
			namepart5 = rand() % 10 + 1;
			cout<<namepart1<<namepart2<<namepart3<<namepart4<<namepart5<<endl;
			cout<<namepart[0][namepart1]<<namepart[1][namepart2]<<namepart[2][namepart3]<<namepart[3][namepart4]<<namepart[4][namepart5]<<endl;
			getch();
		}
		cout<<"Thank you for using the random name generator."<<endl;
		_getch();
	}
	return 0;
}
I have it output the actual numbers so I can see what's going on. As you can see, it generates 5 random numbers, and outputs strings based on the number. This should generate a name that is always longer than five characters. However, sometimes I get names like this:
31010104
Onn
The numbers above the name are the randomly generated numbers. As you can see, the number does not correspond with the name. What am I doing wrong?

Thanks :thup:
 

und34d-rul3

New Member
Reaction score
12
I havn't done C++ in a while so I might not be much help, but is it supposed to outpost something like '31010104' seems like too many numbers, it would be easier to check if you added something like 'Num1=X,Num2=Y...' and so on.

Also you said its supposed to be 5 chars at least, but why can't someone get "A'u"
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
Code:
namepart1 = rand() % 10 + 1;

This gives you numbers between 1 and 10, while your arrays contains entries 0 to 8. Result: nothing good.
 

Knight7770

Hippopotomonstrosesquiped aliophobia
Reaction score
187
Code:
namepart1 = rand() % 10 + 1;

This gives you numbers between 1 and 10, while your arrays contains entries 0 to 8. Result: nothing good.
Now I feel stupid. Thanks :thup:
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top