why does my Program keep getting me an error!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charmeda103
    New Member
    • Oct 2008
    • 36

    why does my Program keep getting me an error!

    My program keeps getting me and error and i dont why

    here is the error message

    • error C2061: syntax error: identifier 'infile'
    • error C2660: 'ReadDate' : function does not take 6 arguments
    • error C2086: 'char &junkChar' : redefinition
    • see declaration of 'junkChar'
    My Program keeps giving me an error and i dont know why





    here is the code
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cmath>
    
    
    
    using namespace std;
    void ReadDate(ifstream&, infile, int&,  char&, int&, char&, int&);
    
    int main()
    
    {
    	ifstream infile("F:\\data2.txt");
    	int numberOfLines;
    	int month;
    	int day;
    	int year;
    	char junkChar;
    
    	infile >>numberOfLines;
    
    	for(int k =1; k<=numberOfLines; k++)
    	{
    		ReadDate(infile, month, junkChar, day, junkChar, year);
    		cout << month << junkChar<< day << junkChar << year << endl;
    	}
    
    
    
    	return 0;
    }
    void ReadDate(ifstream& infile, int&month, char& junkChar, int&day, char& junkChar, int&year)
    {
    	infile >> month >> junkChar >> day >> junkChar >> year;
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    At line #10: get rid of the commas between the type and identifier names.

    kind regards,

    Jos

    Comment

    • charmeda103
      New Member
      • Oct 2008
      • 36

      #3
      hi
      i took out the commas but these two errors messages wont go away.


      error C2086: 'char &junkChar' : redefinition

      see declaration of 'junkChar'

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        You're using 'char& junkChar' twice in your function's parameter list; the second
        one is considered a re-definition.

        kind regards,

        Jos

        Comment

        • charmeda103
          New Member
          • Oct 2008
          • 36

          #5
          what does this mean

          Expression:stri ng subscript out of range

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by charmeda103
            thanks that worked

            i have one more question
            what does this warning mean"

            warning C4552: '+' : operator has no effect; expected operator with side-effect

            this is the part of code where the warning came from.
            Code:
            integerDigit=int(accountNumber[m])-int('0');
            			if(m%2==0)
            			{
            				weightedSum +(2*integerDigit)/10 +(2*integerDigit)%10;
            			}
            			else
            			{
            					weightedSum = weightedSum+integerDigit;
            			
            			}
            There is no assignment (side effect) in the expression in the if-clause.

            kind regards,

            Jos

            Comment

            • charmeda103
              New Member
              • Oct 2008
              • 36

              #7
              whats missing in the if-clause

              Comment

              • boxfish
                Recognized Expert Contributor
                • Mar 2008
                • 469

                #8
                If you want to add something to a variable, use the += operator.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by charmeda103
                  whats missing in the if-clause
                  A side effect (e.g. an assignment operator) in the expression in the if-clause.
                  The compiler was whining about it as I already told you in my previous reply.

                  kind regards,

                  Jos

                  Comment

                  • charmeda103
                    New Member
                    • Oct 2008
                    • 36

                    #10
                    Ok my program calculates all that i need to calculate and for it to work. im reading my data from an infile and writing it into a outfile.

                    the problem is that my program is reading my data only the first line all three times.
                    i want the program to read all three lines of the data and not just the first line 3 times.
                    how do i fix that


                    Code:
                    #include <iostream>
                    #include <string>
                    #include <fstream>
                    #include <cmath>
                    #include <iomanip>
                    
                    
                    using namespace std;
                    void ReadDate(ifstream& infile, int&,  char&, int&, int&);
                    void ReadName(ifstream& infile, string&, string&);
                    void ReadCreditCardNumber(ifstream& infile, string&);
                    void ReadCheckDigit (ifstream& infile, int&);
                    void ReadExpiration(ifstream& infile, int&);
                    int main()
                    
                    {
                    	ifstream infile("F:\\data2.txt");
                    	ofstream outfile("F:\\cardinfo.txt");
                    	int numberOfLines;
                    	int month;
                    	int day;
                    	int year;
                    	char junkChar;
                    	string firstName;
                    	string lastName;
                    	int weightedSum;
                    	int integerDigit;
                    	string accountNumber;
                    	int checkDigit;
                    	int expirationYr;
                    	const string FIRST6_DIGITS = "601100";
                    	const int FIRST6_DIGIT_SUM = 6;	
                    
                    	outfile << " Name " << "                "<< "Card Number" << "               "<< "Expiration Date" << endl;
                    
                    	infile>> numberOfLines;
                    
                    	for(int k =1; k<=numberOfLines; k++)
                    	{
                    		ReadDate(infile, month, junkChar, day, year);
                    		outfile << month << junkChar<< day << junkChar << year << ' ';
                    		ReadName(infile, firstName, lastName);
                    		outfile <<left <<firstName<< ' '<< lastName <<' ';
                    		ReadCreditCardNumber(infile, accountNumber);
                    		
                    
                    		weightedSum=FIRST6_DIGIT_SUM;
                    		for(int m = 0; m < 9; m++)
                    		{
                    			integerDigit= int(accountNumber[m])-int('0');
                    			if(m%2 ==0)
                    			{
                    				weightedSum = (2*integerDigit)/10 +(2*integerDigit)%10;
                    			}
                    			else
                    			{
                    				weightedSum = weightedSum+integerDigit;
                    			
                    			} 
                    		}
                    		checkDigit = 10-weightedSum%10;
                    		if(checkDigit == 10)
                    		{
                    			checkDigit =0;
                    		}
                    		outfile << right<< FIRST6_DIGITS.substr(0,4) << ' ';
                    		outfile << FIRST6_DIGITS.substr(4,2) << accountNumber.substr(0,2) << ' ';
                    		outfile << accountNumber.substr(2,4) << ' ' << accountNumber.substr(6,3) << checkDigit;
                    
                    		ReadExpiration(infile, expirationYr);
                    		
                    		expirationYr=(year + 2)%100;
                    		outfile << right << month/10 << month%10 << '/';
                    		outfile << expirationYr/10 << expirationYr%10 << endl;
                    
                    
                    		
                    
                    
                    
                    		
                    	}
                    	infile.close();
                    	outfile.close();
                    
                    
                    
                    	return 0;
                    }
                    void ReadDate(ifstream& infile, int& month, char& junkChar, int& day,  int& year)
                    
                    {
                    	infile >> month >> junkChar >> day >> junkChar >> year;
                    }
                    void ReadName(ifstream& infile, string& firstName, string& lastName)
                    
                    {
                    	infile >> firstName >> lastName;
                    }
                    void ReadCreditCardNumber(ifstream& infile, string& accountNumber)
                    
                    {
                    	infile >> accountNumber;
                    }
                    void ReadCheckDigit (ifstream& infile, int& checkDigit)
                    
                    {
                    	infile >> checkDigit;
                    }
                    void ReadExpiration(ifstream& infile, int& expirationYr)
                    {
                    	infile >> expirationYr;
                    }

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      What may be happening is that infile has failed. This happens when the file contains data that cannot be stored in the variable you are reading into. Once it has failed, it will not read any more, and the variables you are reading into will contain the values from the last time you used them. I don't know that this is what is happening, but it would cause the same values to be written to outfile over and over. The function infile.fail() will return true if infile has failed. Try writing out the result of a this function to outfile a few times in your program so you can see when or if the file fails. Again, I don't know for sure that this is what's happening, but I hope this helps solve the problem.

                      Comment

                      Working...