Question

Small program designed to allow users to update their details.

All variables initialized in the get functions and get functions are all called before any of the set functions functions called:

int File::Getline1()
{
    std::string filename = std::to_string(user1);

    std::ifstream fin(filename + ".txt");
    fin.getline (line1, 5);
    fin.close();

    user1 = (atoi(line1));

    return user1;
}

int File::Getline2()
{
    std::string filename = std::to_string(user1);

    std::ifstream fin(filename + ".txt");
    fin.getline (line1, 5);
    fin.getline (line2, 5);
    fin.close();

    user2 = (atoi(line2));

    return user2;
 }

long long int File::Getline3()
{
    std::string filename = std::to_string(user1);

    std::ifstream fin(filename + ".txt");
    fin.getline (line1, 5);
    fin.getline (line2, 5);
    fin.getline (line3, 20);
    fin.close();

    char* endptr = NULL;
    return strtoll(line3, &endptr, 10);
}

int File::Getline4()
{
    std::string filename = std::to_string(user1);

    std::ifstream fin(filename + ".txt");
    fin.getline (line1, 5);
    fin.getline (line2, 5);
    fin.getline (line3, 20);
    fin.getline (line4, 5);
    fin.close();

    user4 = (atoi(line4));

    return user4;
}

Which set functions are called is dependent on a series of if statements elsewhere, with SetFile() being called after the if statements.

If Setline3_4 is called, everything is written to the file correctly. However if Setline1 or Setline2 is called, everything writes to file correctly except for user3, which is written as "-3689348814741910324", - user1, user2 and user4 however are written correctly.

I cannot figure out why (note: all user variables are ints, except for user3 which is a long long int - I think maybe the issue lies here?)

void File::Setline1()
{
    user5 = user1;
    filename = std::to_string(user5);
    remove((filename + ".txt").c_str());

    std::cout << "Enter Line 1: ";
    std::cin >> user1;
}

void File::Setline2()
{
    std::cout << "Deposit (can be 0): ";
    std::cin >> deposit;
    std::cout << "\nWithdraw (can be 0): ";
    std::cin >> withdraw;

    user2 = ((user2 + deposit) - withdraw);
}

void File::Setline3_4()
{
    std::cout << "Card Number: ";
    std::cin >> user3;

    std::cout << "Card Expiry date: ";
    std::cin >> user4;
}

void File::Setfile()
{   
    std::string filename = std::to_string(user1);

    std::ofstream fout(filename + ".txt");
    fout << user1 << std::endl << user2 << std::endl << user3 << std::endl << user4 << std::endl;
    fout.close();
}
Was it helpful?

Solution

Because user3 is not initialized. So, it contains some junk value. Just initialize it to avoid junk value printed.

long long user3 = 0 ; // where you have declared user3 in the program.

OTHER TIPS

Option 1 - change the type

You are probably better off using a string to represent 'Card Number', because strictly, its not a 'normal' number that you would perform arithmetic on. This would avoid any numeric conversion issues altogether.

Option 2 - check the input is correctly parsed

However, I can kind-of replicate your problem with the following test code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main(int argc, char *argv[])
{
  long long int A = 1234567890123456;
  long long int B = 9999111122223344;

  cout << "sizeof(A)=" << sizeof(A) << endl;

  cout << "A=" << A << endl;
  cout << "B=" << B << endl;
  cout << "A,B:" << endl << A << endl << B << endl;

  long long int C;
  cout << "Number: ";
  cin >> C;
  cout << "C=" << C << endl;
}

If you enter the 20-digit number 11111111111111111111 the result output is:

C=9223372036854775807

so this is overflowing the maximum value allowed in a long long int, which on my 64-bit system is ~2^63 or 9.2x10^18. In fact if you play a bit you can see it correctly parsing, for example, 9211111111111111111 but failing on 9231111111111111111 (this produces C=9223372036854775807)

So it turns out if you check the iostream failbit, you can see when this happens:

cin >> C;
bool f = cin.fail();
if (f) { cout << "Answer will be wrong." << endl; }
cout << "C=" << C << endl;

So I would say you need check that you actual read the input properly.

Option 3 - check your build environment

I noticed on my system (Linux amd64 gcc 4.7) that the header file for ostream may in some circumstances not handle long long and possibly they get casted to integer.

Lines 197-205 of file /usr/include/c++/4.7/ostream :

#ifdef _GLIBCXX_USE_LONG_LONG
  __ostream_type&
  operator<<(long long __n)
  { return _M_insert(__n); }

  __ostream_type&
  operator<<(unsigned long long __n)
  { return _M_insert(__n); }
#endif

I can repeat your experience with the following code:

#include <iostream>

void function(int x)
{
  std::cout << sizeof(x) << "," << x << std::endl;
}

int main(int argc, char *argv[])
{
  function(12467753);
  long long int Y = 12244444444444444;
  function(Y);
}

This gives the following output:

4,12467753
4,-510310628

Here, no function taking long long int exists but I also had no compiler warning telling me that Y would be truncated to 4 bytes.

So perhaps something in your environment is causing _GLIBCXX_USE_LONG_LONG to not be #defined ? I haven't come across this before myself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top