Error when my program runs

This is a discussion on Error when my program runs within the C++ Programming forums, part of the General Programming Boards category; I would greatly appreciate any help with my simple program. I am a begginner and I don't have much expierience. ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Error when my program runs

    I would greatly appreciate any help with my simple program. I am a begginner and I don't have much expierience. The program I wrote is closed by Windows due to an error when I try to run it after compiling. The "password" part isn't complete but I don't think it should make a difference. Another question I have is about using an instance of a function. I'm not sure how to assign a value to my strings. Please reply with a fix to my problem or any suggestions about changes I should make. (Sorry I was too lazy to make any in depth comments in my code yet)

    Code:
    //I'm writing this program to become more familiar with classes
    //It's supposed to have a basic password thing and then give access to a calculator/average function
    //I'm pretty sure the issue is in the startproc function
    
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    class Calculator
    {
          public:
    
          Calculator();
          ~Calculator();
          void startproc (char user_pass[50], char pass[50] );
          void average (int avnum, int total, int divisor, int answer, char YorN[2]);
    
    };
    
    Calculator::Calculator()
    {}
    
    Calculator::~Calculator()
    {}
    
    void Calculator::startproc (char user_pass[50], char pass[50])
    {    
         ifstream pass_one ( "Pass1.txt" );
         pass_one>> pass;
         cout<< "Please enter the password to access this program.\n";
         cin.getline ( user_pass, 50);
         }
    
    void Calculator::average (int avnum, int total, int divisor, int answer, char YorN[2])
    {    
         while ( strcmp (YorN, "n") == 0)
         {
         cout<<"Please enter the number you would like to average.\n";
         cin>> avnum;
         total = total + avnum;
         cout<<"Are you done entering numbers? (y or n)\n";
         cin.getline(YorN, 2);
         divisor = divisor + 1;
         }
         
         answer = total / divisor;
         cout<<"The average of your numbers is..."<<answer;
         cin.ignore();
         
    }
    
    int main()
    {
        Calculator functions;
        
        functions.startproc(0, 0);
     
        int option;
        
        cout<<"Please select and action..\n";
        cout<<"1) Average\n";
        cin>> option;
        
        
        
        switch (option)
        {
        case 1:
        
        functions.average(0, 0, 1, 0, "null");
        break;
        
        default:
                cout<<"Please select an option.";
                cin.ignore();
                }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you use strings instead of char arrays, it will be easier. First include the header:
    Code:
    #include <string>
    > void startproc (char user_pass[50], char pass[50] );
    > void average (int avnum, int total, int divisor, int answer, char YorN[2]);
    Make the above:
    Code:
          void startproc (string &user_pass, string &pass );
          void average (int avnum, int total, int divisor, int answer, string YorN);
    > cin.getline ( user_pass, 50);
    Make the above:
    Code:
         getline ( cin, user_pass);
    > cin.getline(YorN, 2);
    Make the above:
    Code:
         getline(cin, YorN);
    > while ( strcmp (YorN, "n") == 0)
    Make the above:
    Code:
         while ( YorN == "n")
    To correct the original code, you would need to actually pass char arrays to your member function. For example:
    Code:
    >    functions.startproc(0, 0);
    Would need to take two char arrays:
    Code:
        char pass[50], user_pass[50];
        functions.startproc(pass, user_pass);
    Or you can probably just declare them local to the function:
    Code:
    void Calculator::startproc ()
    {    
         char user_pass[50], pass[50];
         ifstream pass_one ( "Pass1.txt" );
         pass_one>> pass;
         cout<< "Please enter the password to access this program.\n";
         cin.getline ( user_pass, 50);
    }
    Last edited by swoopy; 05-22-2007 at 05:22 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> functions.startproc(0, 0);
    Here, when you call the startproc function, you are passing 0 to the two parameters that expect character arrays. That 0 is considered a null pointer, and so when you try to read into pass it fails. You need to create a character array in main and pass it as an argument to the function (actually you'll need two).

    In fact, why do you have those variables as function parameters in the first place? Another solution is to just make them local variables. The same could be said about the variables in the average function. You don't use them anywhere but in the function and you ignore the values passed in, so they should be local variables.

    Finally, is there a reason you are using C style character arrays instead of C++ strings here? Other than if you have an outdated book/instructor/tutorial, I can't see a reason to use C style strings in this code. They would make things safer and a little bit easier as well. It looks like swoopy beat me to that suggestion with some code help as well.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2
    Thanks guys,
    I just realized that the tutorial I was using mentioned that they were old c style strings. I also did not know that the variables could be declared locally.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Get an Error as soon as the program runs.
    By sara.stanley in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2006, 11:47 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  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