what is wrong with the code?
Mar 26, 2015 at 6:01am UTC
It builds the code but when i run it, it just crashes it says "the stack around the variable 'names' was corrupted"
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream outputfile;
const char CDTitle = 50;
char names[CDTitle];
int count, count2;
const char Artist = 50;
char art[Artist];
int play_length, choice;
//store values in the array
for (count = 0; count < CDTitle; count++)
names[count] = count;
for (count2 = 0; count2 < Artist; count2++)
art[count] = count2;
//open an output file.
outputfile.open("MP3 List.txt" );
//get the information of the mp3 from the user
cout << "Enter the CD title: " ;
cin >> names[CDTitle];
cout << "Enter the artist: " ;
cin >> art[Artist];
cout << "Enter the play length of the song: " << endl;
cin >> play_length;
//write the information to the file.
outputfile << names[CDTitle] << endl;
outputfile << art[Artist] << endl;
outputfile << play_length << endl;
cout << "The file was saved to MP3 list.txt.\n" << endl;
// Close the file
outputfile.close();
cout << "Done and stored.\n" << endl;
return 0;
// Constants for menu choices
const int MP3_choice = 1,
Modify_choice = 2,
Print_choice = 3,
Save_choice = 4,
Retrieve_choice = 5,
Quit_choice = 6;
//Display the interface and get a choice
cout << "\t\tMP3 Menu\n\n"
<< "1. Add MP3\n"
<< "2. Modify a MP3 file\n"
<< "3. Print out MP3 list\n"
<< "4. Save List\n"
<< "5. Retrieve List\n"
<< "6. Quit Program\n"
<< "Enter your choice: " ;
cin >> choice;
//Respond to the user's menu selection
if (choice == MP3_choice)
{
cout << "Enter the CD Title: " ;
cin >> names[CDTitle];
cout << "Enter the artist: " ;
cin >> art[Artist];
cout << "Enter the play length of the song: " << endl;
cin >> play_length;
//write the information to the file.
outputfile << names[CDTitle] << endl;
outputfile << art[Artist] << endl;
outputfile << play_length << endl;
cout << "The file was saved to MP3 list.txt.\n" << endl;
}
else if (choice == Modify_choice)
{
cout << "For what mp3 file do you want to modify " ;
cin >> names[CDTitle] >> art[Artist];
cout << "Enter the new cd name:\n " ;
cin >> names[CDTitle];
cout << "Enter the new artist:\n " ;
cin >> art[Artist];
cout << "Enter the duration:\n " ;
cin >> play_length;
//write the information to the file.
outputfile << names[CDTitle] << endl;
outputfile << art[Artist] << endl;
outputfile << play_length << endl;
cout << "The file was saved to MP3 list.txt.\n" << endl;
}
else if (choice == Print_choice)
{
cout << "Which MP3 do you want to print out? " ;
cout << names[CDTitle] << art[Artist] << play_length << endl;
}
else if (choice == Save_choice)
{
cout << "Which file do you want to save? " ;
outputfile << names[CDTitle] << endl;
outputfile << art[Artist] << endl;
outputfile << play_length << endl;
cout << "The file was saved to MP3 list.txt.\n" << endl;
}
else if (choice == Retrieve_choice)
{
cout << "Which file do you want to retrieve? Please enter a valid file " ;
cin >> names[CDTitle] >> art[Artist] >> play_length;
}
else if (choice == Quit_choice)
{
cout << "Program ending.\n" ;
}
else
{
cout << "The valid choices are 1 through 6. Run the\n"
<< "program again and select one of those.\n" ;
}
return 0;
}
Mar 26, 2015 at 8:05am UTC
After the first loop variable
count will be equal to 50. In the second loop you are using
count as index to the array, but art[50] is out of bounds. You probably meant to use
count2 as index.
To avoid mistakes like this you should declare the variables in the closest scope possible.
1 2 3 4 5 6
int count, count2;
for (int count = 0; count < CDTitle; count++)
names[count] = count;
for (int count = 0; count < Artist; count++)
art[count] = count;
If you do it like that there is no risk you accidentally use the count variables outside the loops.
Mar 26, 2015 at 8:05am UTC
1 2 3 4 5 6 7 8
const char CDTitle = 50;
char names[CDTitle];
//Variable names contains 50 elements with valid indice 0-49
//...
cout << "Enter the CD title: " ;
cin >> names[CDTitle]; //Attempt to access names[50] which is inllegal
Same problem with
art
EDIT: do not use char arrays. Use strings. They lack all those things which baffles beginner programmers.
Last edited on Mar 26, 2015 at 8:06am UTC
Topic archived. No new replies allowed.