C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 15th, 2012, 01:59 AM
fergis911 fergis911 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 fergis911 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 50 sec
Reputation Power: 0
Memory & arrays - Having problem with Array Output

Well I'm working on some Code, and i have most of it done for myself, but when i run the getInfo Function it just keeps writing over my last iteration. I know the solution must be simple but its getting me so aggravated. I've been looking at it for hours and cant seem to spot the problem. So I'm breaking down and asking for any help you guys can offer.


Code:
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <cstdlib>


using namespace std;

struct BookInfo
{
	string title;		//title of book
	double price;		//price of book
};

struct Author
{
	string name;			//author of books
	BookInfo b;		//array to hold info on books author has written
		
}a[];

void showInfo(Author [], int);
void getInfo(Author [], int);

int main()
{
	//variable
	const int SIZE = 3;		//the size of the arrays


	//Array
	Author a[SIZE] = {
		{"NONE", {"NONE", 0}},
		{"NONE", {"NONE", 0}},
		{"NONE", {"NONE", 0}}
	};
	
	
	//Output
	cout << "Here is the data after initialization" << endl;
	showInfo(a, SIZE);
	cout << endl;

	//User Input
	cout << "Get user's input" << endl;
	getInfo(a, SIZE);
	cout << endl;


	//Shows the array when filled with information from user
	cout << "Here is the data after user's input" << endl;
	showInfo(a, SIZE);
	cout << endl;

	system("pause");
	return 0;
}

//shows the information stored in a[]
void showInfo(Author a[], int size)
{
	//displays info for each author
	for (int count1 = 0; count1 < size; count1++)
	{
		cout << "The author: " << a[count1].name << endl;
		//displays info for each book that the author has
		for (int count2 = 0; count2 < size; count2++)
		{
			cout << "\tThe title: " << a[count2].b.title << ", the price: $" << a[count2].b.price << endl;
		}
	}
}

//gets array information from user
void getInfo(Author a[], int size)
{
	//loop to get author name
	for (int count1 = 0; count1 < size; count1++)
	{
		//gets author name from user
		cout << "Enter the author's name: ";
		getline(cin, a[count1].name);
		
		
		//loop to get the title and of the 3 books from user
		for (int count2 = 0; count2 < size; count2++)
		{
			cout << "Enter title "<< (count2 + 1) << " :";
			getline(cin, a[count2].b.title);
			
			//if user enters "NONE" then it kicks out into next Author
			if(a[count2].b.title != "NONE")
			{
				cout << "Enter price " << (count2 + 1) << " : $";
				cin >> a[count2].b.price;
				cin.ignore();
			}else{
				break;
			}
		}		
		cout << endl;
	}
	cout << endl;
}


Sorry if that was a lot but i don't know what parts people might need to help.
All of my problems seem to be when it calls getInfo it overwrites that last bit of data that was input and in the end just displays the same books and prices for all 3 authors.

PS. if you didn't know i am a beginner.

Thanks in advace for any help you can give me.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Having problem with Array Output


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap