Parallel Arrays

OK this is what im trying to do

Write a program that allows you to enter CD names into an array of strings and the CD artist into an array of strings. At the beginning of the program prompt the user to enter the total number of CD’s intended to enter. Max value of 20. After entering the CD names and artists, the application should display the names and artists.

***Your program must work EXACTLY like the one shown below.***

Example Run:
Enter number of CD’s: 3
Enter name of CD 1: The Battle of Los Angeles
Enter the artist of The Battle of Los Angeles: Rage Against the Machine

Enter name of CD 2: So Far So Good
Enter the artist of So Far So Good: Brian Adams

Enter name of CD 3: Amarte es un Placer
Enter the artist of Amarte es un Placer: Luis Miguel

OUTPUT:
(Note: Your output does not need to line up like mine)
CD Names Artists
======== ========
The Battle of Lost Angeles Rage Against the Machine
So Far So Good Brian Adams
Angeles es un Placer Luis Miguel



The problem is its not lining up correctly, and if I put in the CD name as The Battle of Los Angeles <--- With Spaces
it does this:
Enter the total number of CDs (max 20) : 3
Enter name of CD 1: The Battle of Los Angeles
Enter the artist of The: Enter the name of CD 2: Enter the artist of of: Enter name
of CD 3: Enter the artist of Angeles :




But if I enter: WITH NO SPACES it works
Enter the total number of CDs (max 20) : 3

Enter name of CD 1: TheBattleofLosAngelas <------NO SPACES
Enter the artist of TheBattleofLosAngelas: RageAgainsttheMachine <----AGAIN NOSPACES


Enter name of CD 2: SoFarSoGood
Enter the artist of SoFarSoGood: BrianAdams

Enter name of CD 3: AmarteesunPlacer
Enter the artist of AmarteesunPlacer: LuisMiguel







Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

using namespace std;

int main()
{

string cd_names[20];
string cd_artists[20];
int n;
int i;

	cout << "Enter the total number of CDs (max 20): ";
	cin >> n;
	
	cout << endl;

for(i=0;i<n;i++)
{
	cout << "Enter name of CD " << i+1 << ": ";
	cin >> cd_names[i];
	
	cout << "Enter the artist of " + (cd_names[i]) +  ": ";
	cin >> cd_artists[i];
}

	cout << endl;

	cout << "CD Name" << "           " << "Artist" << endl;

for(i=0;i<n;i++)
{	
	cout << cd_names[i] << "           " << cd_artists[i] << endl;
}                      

return 0;

}
Change:
1
2
3
4
5
cin >> n;
// ...
cin >> cd_names[i];
// ...
cin >> cd_artists[i];

To:
1
2
3
4
5
cin.get(n);
// ...
cin.getline(cd_names[i]);
// ...
cin.getline(cd_artists[i]);


A standard cin statement reads data until it reaches white space, a space, return, tab, etc. This is an issue when you're trying to enter a string that might have one of the above in it. cin.get() works the same way, but clears the buffer, IIRC. cin.getline() reads until an end line character, return, is reached, allowing data to be entered and Enter to be pressed once it is all entered.
Topic archived. No new replies allowed.