I'm new to the forum as I've been surfing around for some previously posted help, but I just can't find it. So if anyone can help it would be greatly appreciated.

I'm new to C++ and programming in general and I'm compiling on Microsoft Visual Studio .Net 2003.

For a homework assignment I need to write a program that asks for a user specified number that would be used to determine the number of structures in the array, and then ask the user for the input that would initialize each structure...

this is where i'm at right now, but i'm stuck. I can enter the first catalog.type but after i hit enter the for loop just finishes up without letting me enter anything else...

any pointers in the right direction would be appreciated or something i need to change (please don't just give me the answer... won't learn that way :) )

struct Weapon
{
	char type;
	int ammo;

};

int main(void)
{
	int* total = new int;
	cout << "How many weapons do you wish to catalog? ";
	cin >> *total;
		
	Weapon* catalog = new Weapon[*total];

	for (int i = 0; i < *total; i++)
	{
		cout << "Weapon #" << i + 1 << ":\n"
			<< "Please enter the type of weapon \t\t: ";
		cin.get(catalog[i].type);
		cin.get();
		cout << "Please enter the amount of ammo in stock \t: ";
		cin >> catalog[i].ammo;
		cout << '\n';
		
	
	
	}

	
		

	return 0;
}

<< moderator edit: added [code][/code] tags >>

ok... this is what i've changed it to and now i seem to have more problems... grrrrrr :mad:

struct Weapon
{
	char* type;
	int ammo;

};

int main(void)
{
	int* total = new int;
	cout << "How many weapons do you wish to catalog? ";
	cin >> *total;
		
	Weapon* catalog = new Weapon[*total];

	for (int i = 0; i < *total; i++)
	{
		cout << "Weapon #" << i + 1 << ":\n"
			 << "Please enter the type of weapon \t\t: ";
		char name[50];
		cin.get(name, 50);
		cin.get();
		char* namegun = new char [strlen(name)+1];
		strcpy (namegun, name);
		catalog[1].type = namegun;
		delete [] namegun;


		cout << "Please enter the amount of ammo in stock \t: ";
		cin >> catalog[i].ammo;
		cout << '\n';
	}
	
	cout << "\n"
		<< "Here is a list of weapons in stock: \n";
	for (int i = 0; i < *total; i++)
		cout << catalog[i].type << " has " << catalog[i].ammo << " rounds.\n";
	
	
	
	
		

	return 0;
}

<< moderator edit: added [code][/code] tags >>

Recommended Answers

All 3 Replies

Hi drock9975,
Try this... :cool:

#include <iostream.h>
#include <string.h>

struct Weapon
{
	char* type;
	int ammo;
};

int main(void)
{
	int* total = new int;
	cout << "How many weapons do you wish to catalog? ";
	cin >> *total;

	Weapon* catalog = new Weapon[*total];

	for (int i = 0; i < *total; i++)
	{
		cout << "Weapon #" << i + 1 << ":\n"
		<< "Please enter the type of weapon \t\t: ";
		char name[50];
		cin.get();
		cin.get(name, 50);
		//cin.get();
		char* namegun = new char [strlen(name)+1];
		strcpy (namegun, name);
		//catalog[1].type = namegun;
		catalog[i].type = namegun;
		//delete [] namegun;


		cout << "Please enter the amount of ammo in stock \t: ";
		cin >> catalog[i].ammo;
		cout << '\n';
	}

	cout << "\n"
	<< "Here is a list of weapons in stock: \n";
	//for (int i = 0; i < *total; i++)
	for (i = 0; i < *total; i++)
		cout << catalog[i].type << " has " << catalog[i].ammo << " rounds.\n";

	return 0;
}

Why are you dynamically allocating total why not just use a regular int?

amt_muk, thanks a ton... it makes a boat load of sense now, but in the wee hours of the night I just couldn't see it

prog-bman, you're right also... i was putting too much thought into what i was doing and blah blah blah... thanks for pointing that out

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.