Need help with a c++ program quitting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joshfbm
    New Member
    • Feb 2008
    • 4

    Need help with a c++ program quitting

    So, I wrote this program for a friend. However, after the first variable input by the user, which is the variable "MONSTER", the program unexpectedly quits. Any help would be appreciated. I am using the DMC compiler.

    Code:
    #include <iostream.h>
    
    
    
    //Calculates average percentage of item dropping//
    
    char MONSTER;
    char ITEMDROP;
    char DROPRATE;
    char DROPAMOUNT;
    char MONNUM;
    
    
    int main()
    {
    	cout << "Welcome to Pantherias's Runescape Drop Percentage Calculator.";
    	cout << "This tool will determine how often a item drops after you input"; 
    	cout << "the neccessary data.";
    	cout << "Please enter the monster name, then press ENTER.";
    	cin >> MONSTER;
    	cout << "Now please type the name of the item being dropped,";
    	cout << "then press ENTER.";
    	cin >> ITEMDROP;
    	cout << "After you are satisfied with the fighting,";
    	cout << "type the number of the monster you killed,";
    	cout << "then press ENTER.";
    	cin >> MONNUM;
    	cout << "You are fighting" << MONNUM << " of " << MONSTER << " and attempting to recieve " << ITEMDROP << ".";
    	cout << "Please fight 100 of these monsters, then type in the amount of the item you get";
    	cout << "followed by ENTER.";
    	cin >> DROPAMOUNT;
    	
    	DROPRATE= (DROPAMOUNT*100/MONNUM);
    
    cout << "The " << MONSTER << " dropped  " << ITEMDROP << " approximately " << DROPRATE << " percent of the time over " << MONNUM << " kills.";
    return 0;
    }
  • AmeL
    New Member
    • Oct 2008
    • 15

    #2
    Well, I find nothing wrong with your code beside the incompatibility of the data type u declared and the inputted data.
    Example :
    MONSTER : should be declare as an array of char not char.
    So please reconsider this case again.
    NOTE : One more thing, I would like to say that even your data type should be integer, yet you have to declare it as an array of char to prevent any unexpected error occurred when user input the char type instead of number type.

    Thanks
    /AmeL

    Comment

    • archonmagnus
      New Member
      • Jun 2007
      • 113

      #3
      There are a few things wrong with your code. First, I suggest replacing the depreciated library file "iostream.h " with the more compatible (and useful) "iostream". In other words, first change your first line to read:
      [code=cpp]
      #include <iostream>
      [/code]

      Next, by declaring a variable such as you've done with
      [code=cpp]
      char MONSTER;
      [/code]
      you've created a box named "MONSTER" that holds one (and only one) value. In the case of your code, that value will only be the first letter of what ever you've input. For example, if you have the following code:
      [code=cpp]
      #include <iostream>

      using namespace std;

      int main (void)
      {
      char monster;

      cout<<"Monster name: ";
      cin>>monster;

      cout<<endl
      <<"You entered: "<<monster<<end l;

      return 0;
      }
      [/code]
      you could expect to see the following output:
      [code=text]
      Monster name: Bruxa
      You entered: B

      [/code]

      I'm sure this isn't what you really want. If I may, I'd suggest reading on the use of C++ strings to store the entirety of the input (including any whitespace that be in a monster's name such as the string "Dire Wolf").

      Here's a reference to the C++ string library. You might particularly be interested in the use of getline().

      Comment

      • joshfbm
        New Member
        • Feb 2008
        • 4

        #4
        Alright guys, I'll see what I can do. Thanks for the help, though! This is the first c++ program I've written in a long time, so I'm kind of rusty...but I'm getting back into the hang of it.

        Comment

        Working...