My Program Crashed help!!!

This is a discussion on My Program Crashed help!!! within the C++ Programming forums, part of the General Programming Boards category; Well I just finished the basic V1.0 version of my program using all the infomation I had. I compiled the ...

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    My Program Crashed help!!!

    Well I just finished the basic V1.0 version of my program using all the infomation I had. I compiled the program and then I ran it, 3 seconds later a window pops up and says the program has crashed anyone know why? here my source...


    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    //prototype funtion
    void Showinfo();
    void GetInfo();
    void DisplayResaults();
    
    
    //Call Arrays
    float ItemPrice[128];
    char ItemName[10];
    
    
    
    
    void Showinfo()
    {
    cout << "The KrustyCrab Cash Register.\n  "
         << "Last Updated: September 9th 2003\n\n"
    
         << "Intructions:\n"
         << "Fisrt Enterin A Product name. Then the Price of that  product.\n"
         << "Then when the customer doesent whant anymore products type done for the name. And see the list of products and there prices then the total price.\n\n";
    
      GetInfo();
    }
    
    void GetInfo()
    {
    int i;
    int t;
    
     std::string ItemName[10];
    
    
     cout << "Enter in a product name: ";
      std::getline(std::cin, ItemName[t]);
        std::cout<<"Product Name: "<< ItemName<<std::endl;
    
    
     cout << "Enter in the price: $";
     cin >> ItemPrice[i];
     i++;
     t++;
    
    while (std::getline(std::cin, ItemName[t]) && ItemName[t] != "done")
      {
      DisplayResaults();
      }
    
    
     
    
    
    }
    void DisplayResaults()
    {
    cout << "Product1:"<< ItemName[0]<< "\n"<< "Product1 Price: $"<< ItemPrice[0]<< "\n"
         << "Product2:"<< ItemName[1]<< "\n"<< "Product2 Price: $"<< ItemPrice[1]<< "\n"
         << "Product3:"<< ItemName[2]<< "\n"<< "Product3 Price: $"<< ItemPrice[2]<<"\n"
         << "Product4:"<< ItemName[3]<< "\n"<< "Product4 Price: $"<< ItemPrice[3]<<"\n"
         << "Product5:"<< ItemName[4]<< "\n"<< "Product5 Price: $"<< ItemPrice[4]<<"\n"
         << "Product6:"<< ItemName[5]<< "\n"<< "Product6 Price: $"<< ItemPrice[5]<<"\n"
         << "Product7:"<< ItemName[6]<< "\n"<< "Product7 Price: $"<< ItemPrice[6]<<"\n"
         << "Product8:"<< ItemName[7]<< "\n"<< "Product8 Price: $"<< ItemPrice[7]<<"\n"
         << "Product9:"<< ItemName[8]<< "\n"<< "Product9 Price: $"<< ItemPrice[8]<<"\n"
         << "Product10:"<< ItemName[9]<< "\n"<< "Product10 Price: $"<< ItemPrice[9]<<"\n";
    }
    
    
    
    int main()
    {
      Showinfo();
    
    
      system("pause");
      return 0;
    }
    Thanks for your help...


    ~StormTrooper
    Last edited by St0rmTroop3er; 09-05-2003 at 03:15 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >int i;
    >int t;
    >...
    >std::getline(std::cin, ItemName[t]);
    i and t have no starting value. Accessing them in any way, shape or form is undefined and you're compiler's idea of undefined is a flaming wreck of a program.
    My best code is written with the delete key.

  3. #3
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Okay thanks that helped!!


    AlsoHow do I make it so it keeps asking for Products name and price till there are 10 products or the user inputs done as the Item Name?
    ~Trooper

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Add another condition to the loop:
    Code:
    int n = 0;
    while (std::getline(std::cin, ItemName[t]) && ItemName[t] != "done" && n < 10)
    My best code is written with the delete key.

  5. #5
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Okay I added this to make it loop


    Code:
        int n = 0;
    while (std::getline(std::cin, ItemName[t]) && ItemName[t] != "done" && n < 10)
     {
     GetInfo();
     }
    else
      {
     DisplayResaults();
      }
    }
    but when I enter in the product name it doesent return the product name when I want it it display.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    //prototype funtion
    void Showinfo();
    void GetInfo();
    void DisplayResults();
    
    //Call Arrays
    float  ItemPrice[10];
    string ItemName[10];
    
    void Showinfo()
    {
      cout << "The KrustyCrab Cash Register.\n  "
        << "Last Updated: September 9th 2003\n\n"
        << "Intructions:\n"
        << "Fisrt Enterin A Product name. Then the Price of that  product.\n"
        << "Then when the customer doesent whant anymore products type done for "
        << "the name. And see the list of products and there prices then the total price.\n\n";
      GetInfo();
      DisplayResults();
    }
    
    void GetInfo()
    {
      string name;
    
      for (int i = 0; i < 10; i++) {
        cout << "Enter in a product name: ";
        if (!getline(cin, name) || name == "done")
          break;
        ItemName[i] = name;
        cout << "Enter in the price: $";
        cin>> ItemPrice[i];
        cin.ignore();
      }
    }
    
    void DisplayResults()
    {
      cout << "Product1:"<< ItemName[0]<< "\n"<< "Product1 Price: $"<< ItemPrice[0]<< "\n"
        << "Product2:"<< ItemName[1]<< "\n"<< "Product2 Price: $"<< ItemPrice[1]<< "\n"
        << "Product3:"<< ItemName[2]<< "\n"<< "Product3 Price: $"<< ItemPrice[2]<<"\n"
        << "Product4:"<< ItemName[3]<< "\n"<< "Product4 Price: $"<< ItemPrice[3]<<"\n"
        << "Product5:"<< ItemName[4]<< "\n"<< "Product5 Price: $"<< ItemPrice[4]<<"\n"
        << "Product6:"<< ItemName[5]<< "\n"<< "Product6 Price: $"<< ItemPrice[5]<<"\n"
        << "Product7:"<< ItemName[6]<< "\n"<< "Product7 Price: $"<< ItemPrice[6]<<"\n"
        << "Product8:"<< ItemName[7]<< "\n"<< "Product8 Price: $"<< ItemPrice[7]<<"\n"
        << "Product9:"<< ItemName[8]<< "\n"<< "Product9 Price: $"<< ItemPrice[8]<<"\n"
        << "Product10:"<< ItemName[9]<< "\n"<< "Product10 Price: $"<< ItemPrice[9]<<"\n";
    }
    
    int main()
    {
      Showinfo();
      system("pause");
      
      return 0;
    }
    My best code is written with the delete key.

  7. #7
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    okay thanks


    ~Trooper

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. program won't run standalone
    By richard in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2002, 06:21 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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