Menu

fixed, showpoint, setprecision undeclared

Rusty
2008-02-25
2012-09-26
  • Rusty

    Rusty - 2008-02-25

    Here is my source code. This is from a textbook example. This is not a homework assignment.

    include <iostream>

    include <iomanip>

    include <fstream>

    using namespace std;

    const double MINIMUM_BALANCE = 1000.00;
    const double SERVICE_CHARGE = 25.00;

    int main()
    {
    int intAccountNum;
    double dblBeginningBal;
    double dblAccountBal;

    double dblAmountDep = 0.0;
    int intNumberDep = 0;

    double dblAmountWithd = 0.0;
    int intNumberWithd = 0;
    double dblInterestPaid = 0.0;

    char chTransactionCode;
    double dblTransactionAmount;

    bool blnIsServiceCharge = false;

    ifstream infile;
    ofstream outfile;

    infile.open("C:\input_text.txt");

    if (!infile)
    {
    cout << "Cannot open the input file.\n"
    << "Press 'Enter' to terminate program.\n";
    cin.get() ;
    return 1;
    }

    outfile.open("C:\output_file.out");

    // I get errors here at line 50
    outfile << fixed << showpoint;

    // *** Setprecision appears to have no effect, though I get no error
    //outfile << setprecision(2);

    cout << "Processing Data" << endl;

    dblAccountBal = dblBeginningBal;

    infile >> chTransactionCode >> dblTransactionAmount;

    while (infile)
    {

    switch (chTransactionCode)
    {
    case 'D':
    case 'd':
    dblAccountBal += dblTransactionAmount;
    dblAmountDep += dblTransactionAmount;
    intNumberDep++;
    break;
    case 'I':
    case 'i':
    dblAccountBal += dblTransactionAmount;
    dblInterestPaid += dblTransactionAmount;
    break;
    case 'W':
    case 'w':
    dblAccountBal -= dblTransactionAmount;
    dblAmountWithd += dblTransactionAmount;
    intNumberWithd++;

     if ((dblAccountBal &lt; MINIMUM_BALANCE) &amp;&amp; (!blnIsServiceCharge))
     {
     blnIsServiceCharge = true;
     dblAccountBal -= SERVICE_CHARGE;
     }
     break;
    

    default:
    cout << "Invalid transaction code.\n";
    } //end switch

    infile >> chTransactionCode >> dblTransactionAmount;
    } //end while

    outfile << "Account Number: "<< intAccountNum << endl;
    outfile << "Beginning Balance: $" << dblBeginningBal << endl;
    outfile << "Ending Balance: $" << dblAccountBal << endl << endl;
    outfile << "Interest Paid: $" << dblInterestPaid << endl << endl;
    outfile << "Amount Deposited: $" << dblAmountDep << endl;
    outfile << "Number of Deposits: " << intNumberDep << endl << endl;
    outfile << "Amount Withdrawn: $" << dblAmountWithd << endl;
    outfile << "Number of Withdrawals: " << intNumberWithd << endl << endl;
    if (blnIsServiceCharge)
    outfile << "Service Charge: $" << SERVICE_CHARGE << endl;

    return 0;
    }

    The problem is that in the output file (output_file.out) I get these results if I comment only line 50, which instructs the compiler to use fixed and showpoint. This is where you can see that setprecision appears to have no effect.

    After compiling the program, output_file.out looks as follows:

    Account Number: 37879712
    Beginning Balance: $1.4e-309
    Ending Balance: $7.2e+02

    Interest Paid: $2.5e+02

    Amount Deposited: $2.2e+03
    Number of Deposits: $3

    Amount Withdrawn: $1.7e+03
    Number of Withdrawals: 6

    Service Charge: $25

    Also, is it even possible to use directory names with spaces? I know that this is an issue, but is there anything I can do? Eg:
    change:
    C:\Documents and Settings\Russell Estes\My Documents\CPP\input_text.txt
    to...
    C:\Documents%and%Settings\Russell%Estes\My Documents\CPP\input_text.txt

    Thank you for your help!

     
    • cpns

      cpns - 2008-02-26

      Strictly:

      const char *x = "...path";

      Aparet from that I am not sure what you or kip2847 are actually trying to solve since paths with spaces are not a problem in your own code unless you want to port the code to an OS that does not support them. The problem relates merely to their use by the GNU toolchain which is ported from an OS that doesn't support them (at least not in quite the same way, before someone points out the %20 kludge).

      You could also execute a subst command from the command line:

      subst z: C:\Documents and Settings\Russell Estes\My Documents\CPP\

      will create a 'virtual' drive z: so your file will be at z:\input_text.txt

      >> btw if you try using a string as an arguement
      >> instead of a char it probbly wont work
      Hardly a problem!:

      std::string x = "...path";
      fstream file( x.c_str() );

      >> // I get errors here at line 50
      What errors! Post the log! Compiles in VC++ 2008 with the following warnings:

      main.cpp(54) : warning C4700: uninitialized local variable 'dblBeginningBal' used
      main.cpp(94) : warning C4700: uninitialized local variable 'intAccountNum' used

      you'll want to fix those!

       
    • kip2847

      kip2847 - 2008-02-26

      I can probbly solve your problem with the spaces in the path name.
      What i do is declare the path as a char and place the varible as an arguement in the file declaration;
      i.e.
      char
      x = "...path";
      fstream file( *x);

      This wil probbly work.
      btw if you try using a string as an arguement instead of a char it probbly wont work bc the stream class is not overloaded to take string arguments.

      Hope this works

       
    • Rusty

      Rusty - 2008-02-26

      hmm... are you declaring a pointer that points to a memory location of type char? whats with the asterisk before the char variable name. Also, aren't char variables only able to hold one character at a time? Thanks a lot Kip for your reply!

       
    • kip2847

      kip2847 - 2008-02-26

      char [varible] holds more then one character since its a pointer. You should read up on pointers.
      btw there is a getline() function you can also use to get a string with spaces in it and returns a char
      .

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.