What's wrong with this code?

This is a discussion on What's wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; int main() { string songs[4]; int song2; string song; int index; for ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    What's wrong with this code?

    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
    
    int main() 
    { 
    string songs[4]; 
    int song2; 
    string song; 
    int index; 
    
    for (index = 0; index < 5; index ++) 
    { 
    cout << "please enter the name of song number " << index + 1; 
    cin >> song; 
    songs[index] = song; 
    } 
    
    cout << "Please enter the number of the song that you want"; 
    cin >> song2; 
    cout << endl << "you have chosen " << songs[0]; 
    
    }
    Can someone please help me find what's wrong with this code. I know it's not a syntax error because the program runs, but it comes up with an error as soon as i've eneterd the fifth song.

    It says "Debug assertaion failed!"

    Anyone else had this problem? Please help.

    Thanks.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here, you are exceeding the upper bound of your array:
    Code:
    for (index = 0; index < 5; index ++)
    should be:
    Code:
    for (index = 0; index < 4; index ++)
    Here is one recommendation, try this:
    Code:
    cout << "Please enter the number of the song that you want"; 
    cin >> song2; 
    cout << endl << "you have chosen " << songs[0];
    change to:
    Code:
    cout << "Please enter the number of the song that you want"; 
    cin >> song2; 
    cout << endl << "you have chosen " << songs[song2-1];   //will adjust the user's choice to return the desired element from the array
    Last edited by The Brain; 11-01-2006 at 04:55 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    it runs fine on my compiler. maybe supply your compiler name and version as well as OS info.

    only thing i could think of to change is:
    cin >> song;
    songs[index] = song;
    to
    cin >> songs[index];

    again, it works without any changes on my computer..

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by nadroj
    it runs fine on my compiler.
    Dangerous words. The original code writes data beyond the end of an array.

    The original code has an array that is 4 elements long (0, 1, 2, 3).
    Then it accesses 5 elements (0, 1, 2, 3, 4).

    That last element is beyond the end of the array. It's actually better if the program DOES crash; it's a serious error and it's better for the programmer to find and catch it.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    initially when i looked at the code i figured it must be an out of bounds type of error.. i some how missed it! even after reading it over afew times. im aware of the problems this program can run and i see the logic error now, dont know how i missed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21