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 >>

Dani AI

Generated

Several straightforward C++ issues are present in the thread that explain the early loop termination and strange output: mixing formatted extraction (>>) with line-oriented reads leaves a newline in the input buffer; assigning every name to catalog[1] instead of catalog[i] is an index bug; deleting the temporary buffer right after assigning its pointer to the struct produces a dangling pointer; and allocating a single int with new is unnecessary. fixed the immediate symptom (the input-order/consumption), and correctly pointed out the pointless new int.

A more robust approach uses automatic storage and RAII: make the struct hold std::string for the weapon type and use std::vector for a dynamic collection. Read whole lines with std::getline and call std::cin.ignore() after numeric >> reads to remove the leftover newline. Example pattern (not present in earlier posts):

#include <iostream>
#include <string>
#include <vector>

struct Weapon { std::string type; int ammo; };

int main() {
    int total;
    std::cout << "How many weapons? ";
    std::cin >> total;
    std::cin.ignore(); // remove newline before getline

    std::vector<Weapon> catalog(total);
    for (int i = 0; i < total; ++i) {
        std::cout << "Type: ";
        std::getline(std::cin, catalog[i].type);
        std::cout << "Ammo: ";
        std::cin >> catalog[i].ammo;
        std::cin.ignore(); // prepare for next getline
    }
}

If constrained to C-style strings, allocate one buffer per element and free it only when the element is no longer required—do not delete[] the buffer immediately after assigning its pointer into the array (that creates a dangling pointer). If new[] is used for the catalog, remember to delete[] it at program end; preferably avoid that by using std::vector. For debugging, print the loop index and the raw buffer state when input looks wrong; that will quickly reveal off-by-one indexing and leftover-newline problems.

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.