Below is my homework program. We’re supposed to output a list of books and authors, using two parallel string arrays.
I’m completely confused about using strings in arrays (or strings, period). I’m using a different version of the book, which was all right until we hit arrays. Apparently the newer book teaches strings before arrays, but mine has arrays before strings. I skimmed through all of the Strings chapter, but I don’t see how to do arrays with strings.
I know it’s got to be very basic, but I’ve worked every spare moment on this program, and now I just keep getting more confused than anything.
Program Requirements: The user will enter a book’s title and then the book’s author. The user may enter up to and including ten books’ titles and authors. The program will output each book’s title and respective author on one line per book. Note that first the user must enter the number of books to be entered, before the arrays are populated
The chapter we’re on is one-dimensional arrays, if that makes a difference. Thank you for any help.
#include <iostream>
#include <string>
usingnamespace std;
//Function Prototype
void displayBookInfo(string title[10], string author[10], int numElements);
int main()
{
//Declare Variable
int number = 0;
//Declare Parallel Arrays
string bookTitle[10];
string bookAuthor[10];
//Get Number of Books from User
//Maximum number of Books to Be Entered = 10
cout << "Enter number of books: ";
cin >> number;
if (number > 10)
{
cout << "You must enter a number between 1 and 10." << endl << endl;
}
else
{
//Fill Arrays
for (int x = 0; x < number; x += 1)
{
do
{
cout << "Enter the name of Book #" << x + 1 << ": ";
cin >> bookTitle[x];
cout << "Enter the author's name of " << bookTitle[x] << ": ";
cin >> bookAuthor[x];
x += 1; //update counter
} while (x < number); //end do...while loop
} //end for
} //end else
cout << endl << endl;
//Display Contents of Array
displayBookInfo(bookTitle, bookAuthor, 10);
system("pause");
return 0;
} //end of main function
//*****Function Definition*****
void displayBookInfo(string title[10], string author[10], int numElements) //function header
{
for (int x = 0; x < numElements; x += 1)
{
cout << title[x] << "'\t''\t''\t'" << author[x] << endl;
} //end for
} //end of displayBookInfo function
1. When asking for book information you have a nested do/while loop inside a for loop, you only need the outer for loop
1 2 3 4 5 6 7 8
//Fill Arrays
for (int x = 0; x < number; x += 1)
{
cout << "Enter the name of Book #" << x + 1 << ": ";
cin >> bookTitle[x];
cout << "Enter the author's name of " << bookTitle[x] << ": ";
cin >> bookAuthor[x];
} //end for
2.
When you call displayBookInfo(bookTitle, bookAuthor, 10);
replace 10 with number displayBookInfo(bookTitle, bookAuthor, number);
#include <iostream>
#include <string>
usingnamespace std;
//Function Prototype
void displayBookInfo(string title[10], string author[10], int numElements);
int main()
{
//Declare Variable
int number = 0;
//Declare Parallel Arrays
string bookTitle[10] = {""};
string bookAuthor[10] = {""};
//Get Number of Books from User
//Maximum number of Books to Be Entered = 10
cout << "Enter the number of books: ";
cin >> number;
if (number > 10)
{
cout << "You must enter a number between 1 and 10." << endl << endl;
}
else
{
//Fill Arrays
for (int x = 0; x < number; x += 1)
{
cin.ignore(100, '\n');
cout << "Enter the name of Book #" << x + 1 << ": ";
getline(cin, bookTitle[x]);
cin.ignore(100, '\n');
cout << "Enter the author's name of " << bookTitle[x] << ": ";
getline(cin, bookAuthor[x]);
} //end for
} //end else
cout << endl << endl;
//Display Contents of Arrays
displayBookInfo(bookTitle, bookAuthor, number); //function call
system("pause");
return 0;
} //end of main function
//*****Function Definition*****
void displayBookInfo(string title[10], string author[10], int numElements) //function header
{
for (int x = 0; x < numElements; x += 1)
{
cout << title[x] << "'\t''\t''\t'" << author[x] << endl;
} //end for
} //end of displayBookInfo function
And here's some output:
Enter the number of books: 2
Enter the name of Book #1: Book One
Enter the author's name of Book One: Author One
Enter the name of Book #2: Book Two
Enter the author's name of Book Two: Author Two
Book One' '' '' 'Author One
Book Two' '' '' 'Author Two
Press any key to continue . . .
And here's output for what happens if I enter > 10 on line 20:
Enter the number of books: 12
You must enter a number between 1 and 10.
' '' '' '
' '' '' '
' '' '' '
' '' '' '
' '' '' '
' '' '' '
' '' '' '
' '' '' '
' '' '' '
' '' '' '
And here are the problems with my program:
1. When testing the if clause and entering > 10, my program flips out, and then I get the BREAK/CONTINUE/IGNORE popup saying this: Unhandled exception at 0x60ac1f98 (msvcp100d.dll) in bookArrays.exe: 0xC0000005: Access violation reading location 0xcccccccc.
2. Also, in the for loop, I don't understand why the program requires two times of hitting Enter, before the title and author prompts will appear.
3. And why do those apostrophes appear? It happens whether or not I initialize the arrays to empty strings.
Thank you for any clues and help. I'm totally new at strings and arrays and C++ language.
#include <iostream>
#include <string>
usingnamespace std;
//Function Prototype
void displayBookInfo(string title[10], string author[10], int numElements);
int main()
{
//Declare Variable
int number = 0;
//Declare Parallel Arrays
string bookTitle[10];
string bookAuthor[10];
//Get Number of Books from User
//Maximum number of Books to Be Entered = 10
cout << "Enter the number of books: ";
cin >> number;
//Fill Arrays
for (int x = 0; x < number; x += 1)
{
cin.ignore(10, '\n');
cout << "Enter the name of Book #" << x + 1 << ": ";
getline(cin, bookTitle[x]);
cin.ignore(100, '\n');
cout << "Enter the author's name of " << bookTitle[x] << ": ";
getline(cin, bookAuthor[x]);
} //end for
cout << endl << endl;
//Display Contents of Arrays
displayBookInfo(bookTitle, bookAuthor, number); //function call
system("pause");
return 0;
} //end of main function
//*****Function Definition*****
void displayBookInfo(string title[10], string author[10], int numElements) //function header
{
for (int x = 0; x < numElements; x += 1)
{
cout << title[x] << "'\t''\t''\t'" << author[x] << endl;
} //end for
} //end of displayBookInfo function
Concerning lines 26-31, why is the program requiring two presses of the Enter key before the cout statements (on lines 27 and 30) appear?
What do I need to do so the cout statements will appear after the usual one press of Enter?
try changing cin.ignore(100, '\n') to cin.ignore(10, '\n') on line 29. sometimes this happens to me as well when I set the number too high.
*edit*
in fact seeing as this is a string array and nothing will be left in cin change it to cin.ignore(1, '\n') or just cin.ignore('\n')
and as for the quotations appearing in your final output, it's because of the line cout<<title[x]<<"'\t''\t''\t'"<<author[x]<<endl;
when you put the \t in double quotes you don't need the single quotes in between, so the program is displaying 1 single quote, tabbing over, displaying the 2 single quotes, tabs over again, displays 2 more single quotes, tabs over a third time and displays a single quote again. so change the line to