C++ help please

Discussion in 'OT Technology' started by High Voltage, Feb 5, 2008.

  1. High Voltage

    High Voltage Guest

    I am trying to get the bonus question for my homework assignment but have no idea what to do. the question is "prompt user for name of input file and use that filename"

    this is my program i have written.

    Code:
    [SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]<fstream>[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]<iomanip>[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]<string>[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][SIZE=2][COLOR=#000000] std;[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2][COLOR=#000000] main()[/COLOR][/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] grossamount = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare gross amount[/COLOR][/SIZE]
    [SIZE=2]string name; [/SIZE][SIZE=2][COLOR=#008000]// declare name as a string[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] federalincometax = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare federal income tax[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] statetax = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare state tax[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] sstax = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare social security tax[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] medtax = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare medicare/medacaid tax[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] penplan = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare pension plan[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] healthins = 75; [/SIZE][SIZE=2][COLOR=#008000]// declare health insurance[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][SIZE=2] netpay = 0; [/SIZE][SIZE=2][COLOR=#008000]// declare netpay[/COLOR][/SIZE]
    [SIZE=2][COLOR=#008000]// declare the stream names[/COLOR][/SIZE]
    [SIZE=2]ifstream infile;[/SIZE]
    [SIZE=2]ofstream outfile;[/SIZE]
    [SIZE=2][COLOR=#008000]// declare the stream files[/COLOR][/SIZE]
    [SIZE=2]infile.open([/SIZE][SIZE=2][COLOR=#a31515]"indata.txt"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]outfile.open([/SIZE][SIZE=2][COLOR=#a31515]"outdata.txt"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2][COLOR=#008000]// precision set and no E notation[/COLOR][/SIZE]
    [SIZE=2]outfile << showpoint << fixed; [/SIZE]
    [SIZE=2]outfile << setprecision(2); [/SIZE]
    [SIZE=2][COLOR=#008000]// get the variable gross amount from the input file[/COLOR][/SIZE]
    [SIZE=2]infile >> grossamount;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// prompt user for name (extra credit part)[/COLOR][/SIZE]
    [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"Enter Name: "[/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]getline(cin, name);[/SIZE]
    [SIZE=2][COLOR=#008000]// process...do the math[/COLOR][/SIZE]
    [SIZE=2]federalincometax = (grossamount * .15); [/SIZE][SIZE=2][COLOR=#008000]// fed tax[/COLOR][/SIZE]
    [SIZE=2]statetax = (grossamount * .035); [/SIZE][SIZE=2][COLOR=#008000]// state tax[/COLOR][/SIZE]
    [SIZE=2]sstax = (grossamount * .0575); [/SIZE][SIZE=2][COLOR=#008000]// social security tax[/COLOR][/SIZE]
    [SIZE=2]medtax = (grossamount * .0275); [/SIZE][SIZE=2][COLOR=#008000]// medicare tax[/COLOR][/SIZE]
    [SIZE=2]penplan = (grossamount * .05); [/SIZE][SIZE=2][COLOR=#008000]// pension plan[/COLOR][/SIZE]
    [SIZE=2]netpay = grossamount - (federalincometax + statetax + sstax + medtax + penplan + healthins); [/SIZE][SIZE=2][COLOR=#008000]//net pay[/COLOR][/SIZE]
    [SIZE=2][COLOR=#008000]// Print Name[/COLOR][/SIZE]
    [SIZE=2]outfile << name << endl;[/SIZE]
    [SIZE=2][COLOR=#008000]// Print Gross Amount[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Gross Amount: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << grossamount << endl;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// Print blank line[/COLOR][/SIZE]
    [SIZE=2]outfile << [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
    [SIZE=2][COLOR=#008000]// Print Federal Taxes[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Federal Tax: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << federalincometax << endl;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// Print State Taxes[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"State Tax: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << statetax << endl;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// Print Social Security Taxes[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Social Security Tax: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << sstax << endl;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// Print Medicare/Medicaid Taxes[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Medicare/Medicaid Tax: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << medtax << endl;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// Print Pension Plan[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Pension Plan: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << penplan << endl;[/SIZE]
     
    [SIZE=2][COLOR=#008000]// Print Health insurance[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Health Insurance: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << healthins << endl;[/SIZE]
    [SIZE=2][COLOR=#008000]// Print blank line[/COLOR][/SIZE]
    [SIZE=2]outfile << [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
    [SIZE=2][COLOR=#008000]// Pring gross pay[/COLOR][/SIZE]
    [SIZE=2]outfile << left << setw(35) << setfill([/SIZE][SIZE=2][COLOR=#a31515]'.'[/COLOR][/SIZE][SIZE=2])<< [/SIZE][SIZE=2][COLOR=#a31515]"Net Pay: "[/COLOR][/SIZE][SIZE=2] << [/SIZE][SIZE=2][COLOR=#a31515]" $"[/COLOR][/SIZE][SIZE=2] << netpay << endl;[/SIZE]
     
     
    [SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
    [SIZE=2]}[/SIZE]
     
    
    thanks for your help :)
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    [FONT=&quot]char filename [81];[/FONT]
    [FONT=&quot]cout << “Enter the name of the input file: “;[/FONT]
    [FONT=&quot]cin >> filename;[/FONT]
    [FONT=&quot]inFile.open (filename);[/FONT]



    first result in google for "opening a user defined file C++"
     
  3. High Voltage

    High Voltage Guest

    thank you sir.
     
  4. CodeX

    CodeX Guest

    It would be more impressive if you actually checked that:
    A - The extension of the filename given matched what your program needs
    B - The file name is actually valid and exists on the drive
    C - If you want to get really fancy add a unique ID string to the beginning of each file your program saves so you can tell automatically when you try to open it next if it is a valid file for your program.
     

Share This Page