By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,497 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,497 IT Pros & Developers. It's quick & easy.

why does my Program keep getting me an error!

P: 36
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <cmath>
  6.  
  7.  
  8.  
  9. using namespace std;
  10. void ReadDate(ifstream&, infile, int&,  char&, int&, char&, int&);
  11.  
  12. int main()
  13.  
  14. {
  15.     ifstream infile("F:\\data2.txt");
  16.     int numberOfLines;
  17.     int month;
  18.     int day;
  19.     int year;
  20.     char junkChar;
  21.  
  22.     infile >>numberOfLines;
  23.  
  24.     for(int k =1; k<=numberOfLines; k++)
  25.     {
  26.         ReadDate(infile, month, junkChar, day, junkChar, year);
  27.         cout << month << junkChar<< day << junkChar << year << endl;
  28.     }
  29.  
  30.  
  31.  
  32.     return 0;
  33. }
  34. void ReadDate(ifstream& infile, int&month, char& junkChar, int&day, char& junkChar, int&year)
  35. {
  36.     infile >> month >> junkChar >> day >> junkChar >> year;
  37. }
  38.  
  39.  
Nov 2 '08 #1
Share this Question
Share on Google+
10 Replies


Expert 10K+
P: 11,448
At line #10: get rid of the commas between the type and identifier names.

kind regards,

Jos
Nov 2 '08 #2

P: 36
hi
i took out the commas but these two errors messages wont go away.


error C2086: 'char &junkChar' : redefinition

see declaration of 'junkChar'
Nov 2 '08 #3

Expert 10K+
P: 11,448
You're using 'char& junkChar' twice in your function's parameter list; the second
one is considered a re-definition.

kind regards,

Jos
Nov 2 '08 #4

P: 36
what does this mean

Expression:string subscript out of range
Nov 2 '08 #5

Expert 10K+
P: 11,448
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. integerDigit=int(accountNumber[m])-int('0');
  3.             if(m%2==0)
  4.             {
  5.                 weightedSum +(2*integerDigit)/10 +(2*integerDigit)%10;
  6.             }
  7.             else
  8.             {
  9.                     weightedSum = weightedSum+integerDigit;
  10.  
  11.             }
  12.  
There is no assignment (side effect) in the expression in the if-clause.

kind regards,

Jos
Nov 2 '08 #6

P: 36
whats missing in the if-clause
Nov 2 '08 #7

boxfish
Expert 100+
P: 469
If you want to add something to a variable, use the += operator.
Nov 2 '08 #8

Expert 10K+
P: 11,448
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
Nov 2 '08 #9

P: 36
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


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <iomanip>
  6.  
  7.  
  8. using namespace std;
  9. void ReadDate(ifstream& infile, int&,  char&, int&, int&);
  10. void ReadName(ifstream& infile, string&, string&);
  11. void ReadCreditCardNumber(ifstream& infile, string&);
  12. void ReadCheckDigit (ifstream& infile, int&);
  13. void ReadExpiration(ifstream& infile, int&);
  14. int main()
  15.  
  16. {
  17.     ifstream infile("F:\\data2.txt");
  18.     ofstream outfile("F:\\cardinfo.txt");
  19.     int numberOfLines;
  20.     int month;
  21.     int day;
  22.     int year;
  23.     char junkChar;
  24.     string firstName;
  25.     string lastName;
  26.     int weightedSum;
  27.     int integerDigit;
  28.     string accountNumber;
  29.     int checkDigit;
  30.     int expirationYr;
  31.     const string FIRST6_DIGITS = "601100";
  32.     const int FIRST6_DIGIT_SUM = 6;    
  33.  
  34.     outfile << " Name " << "                "<< "Card Number" << "               "<< "Expiration Date" << endl;
  35.  
  36.     infile>> numberOfLines;
  37.  
  38.     for(int k =1; k<=numberOfLines; k++)
  39.     {
  40.         ReadDate(infile, month, junkChar, day, year);
  41.         outfile << month << junkChar<< day << junkChar << year << ' ';
  42.         ReadName(infile, firstName, lastName);
  43.         outfile <<left <<firstName<< ' '<< lastName <<' ';
  44.         ReadCreditCardNumber(infile, accountNumber);
  45.  
  46.  
  47.         weightedSum=FIRST6_DIGIT_SUM;
  48.         for(int m = 0; m < 9; m++)
  49.         {
  50.             integerDigit= int(accountNumber[m])-int('0');
  51.             if(m%2 ==0)
  52.             {
  53.                 weightedSum = (2*integerDigit)/10 +(2*integerDigit)%10;
  54.             }
  55.             else
  56.             {
  57.                 weightedSum = weightedSum+integerDigit;
  58.  
  59.             } 
  60.         }
  61.         checkDigit = 10-weightedSum%10;
  62.         if(checkDigit == 10)
  63.         {
  64.             checkDigit =0;
  65.         }
  66.         outfile << right<< FIRST6_DIGITS.substr(0,4) << ' ';
  67.         outfile << FIRST6_DIGITS.substr(4,2) << accountNumber.substr(0,2) << ' ';
  68.         outfile << accountNumber.substr(2,4) << ' ' << accountNumber.substr(6,3) << checkDigit;
  69.  
  70.         ReadExpiration(infile, expirationYr);
  71.  
  72.         expirationYr=(year + 2)%100;
  73.         outfile << right << month/10 << month%10 << '/';
  74.         outfile << expirationYr/10 << expirationYr%10 << endl;
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.     }
  83.     infile.close();
  84.     outfile.close();
  85.  
  86.  
  87.  
  88.     return 0;
  89. }
  90. void ReadDate(ifstream& infile, int& month, char& junkChar, int& day,  int& year)
  91.  
  92. {
  93.     infile >> month >> junkChar >> day >> junkChar >> year;
  94. }
  95. void ReadName(ifstream& infile, string& firstName, string& lastName)
  96.  
  97. {
  98.     infile >> firstName >> lastName;
  99. }
  100. void ReadCreditCardNumber(ifstream& infile, string& accountNumber)
  101.  
  102. {
  103.     infile >> accountNumber;
  104. }
  105. void ReadCheckDigit (ifstream& infile, int& checkDigit)
  106.  
  107. {
  108.     infile >> checkDigit;
  109. }
  110. void ReadExpiration(ifstream& infile, int& expirationYr)
  111. {
  112.     infile >> expirationYr;
  113. }
  114.  
Nov 2 '08 #10

boxfish
Expert 100+
P: 469
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.
Nov 2 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.