Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Having trouble getting this to display properly
the user has the option to add up to 10 coins then the option to display the coins
i can get it to work if i know how many coins are input using a for loop but for
some reason this has me stump. Any help will be greatly appreciated

C++
// Coins.h
#ifndef COINS_H
#define COINS_H
#include <iostream>
#include <string>
using namespace std;

struct Coins
{
	int date;
	string type;
	string country;
};
#endif

// driver.cpp
#include "Coins.h"

void getCoins(Coins coins[], int SIZE, int count);
void displayCoins(Coins coins[], int SIZE, int count);
const int SIZE = 10;

int main()
{
	Coins coins[SIZE];
	char answer;
	char displayAnswer;
	int count = 0;

	cout << "Enter up to 10 coins.\n"
		 << "Enter a coin (Y/N) ? ";
	cin >> answer;
	cout << endl;
	system("CLS");

	if (toupper(answer) == 'Y')
	{
		count = 1;
		getCoins(coins, SIZE, count);
	}
	cout << "Display the coin(s) (Y/N) ? ";
	cin >> displayAnswer;

	if (toupper(answer) == 'Y')
		displayCoins(coins, SIZE, count);

	return 0;
}
//======================================
void getCoins(Coins coins[], int SIZE, int count)
{
	char again = 'Y';
	do {
		for (int i = 0; i < 1; i++)
		{
			cout << "DATE: ";
			cin >> coins[i].date;
			cin.ignore();
			cout << "TYPE: ";
			getline(cin, coins[i].type);
			cout << "COUNTRY: ";
			getline(cin, coins[i].country);
			//continue;
		} 
		cout << "Enter another coin (Y/N) ? ";
		cin >> again;
		system("CLS");

		if (toupper(again) == 'Y')
			count++;
	} while (toupper(again) != 'N');
	/*cout << "DATE: ";
	cin >> coins->date;
	cin.ignore();
	cout << "TYPE: ";
	cin >> coins->type;
	cout << "COUNTRY: ";
	cin >> coins->country;*/
}
//======================================
void displayCoins(Coins coins[], int SIZE, int count)
{
	cout << "There are " << count << " coins\n\n";

	for (int i = 0; i < count; i++)
	{
		cout << '#' << i + 1 << ".) \n";
		cout << coins[i].date << endl
			 << coins[i].type << endl
			 << coins[i].country << endl;

	}
	/*cout << coins->date << endl
		 << coins->type << endl
		 << coins->country << endl;*/
}


What I have tried:

Having trouble getting this to display properly
the user has the option to add up to 10 coins then the option to display the coins
i can get it to work if i know how many coins are input using a for loop but for
some reason this has me stump. Any help will be greatly appreciated
Posted
Updated 15-Mar-16 13:38pm
v2
Comments
barneyman 15-Mar-16 19:29pm    
two observations

1. you don't do any bounds checking (ie when count==SIZE)
2. how does displayCoins know how many coins were input in getCoins, magic?

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

I see 2 things:
Its is not necessary to have SIZE given as parameter in functions as it is already define as a global value.
You count coins in getCoins, but the value never go to count in main.
 
Share this answer
 
Comments
Member 12394645 15-Mar-16 20:13pm    
Thank you for clearing that up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900