Hey all,
I am working on my first C++ prog and I'm having an issue on my output. I compiled the code ok but when i try to execute, the output comes up with weird numbers and letters where it is supposed to display the "photos in"
// Name: Danny Delgado
// Course: CIS-165
// Last Update: 2/21/2012
// Description: Moreprints Photo Prints
//*********************** Preprocessor Directives *********************
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
//******************************* main ********************************
int main(void)
{
double photos, // photos entered by customer
reprints, // reprints submitted by customer
total_photos, // total photos calculated product of photos x reprints
cost_prints, // total photos x .50
tax_total, // cost of prints * .07
price_total, // cost_prints + tax_total
tax_rate = .07, // tax rate
price_per = .50, // price per photo
choice;
string name; // entered name
char drive[2],
disk_file[15],
file[9];
ofstream outfile;
// input section
system ("cls"); // clear the screen
cout << "please enter your first and last name" << endl;
cin >> name;
cout << "please enter number of photos" << endl << endl;
cin >> photos;
cout << "please enter reprints submitted" <<endl;
cin >> reprints;
// process section
total_photos = photos * reprints;
cost_prints = total_photos * price_per;
tax_total = cost_prints * tax_rate;
price_total = cost_prints + tax_total;
// output section
system ("cls");
cout << "Output to console (1) or disk file (2): ";
cin >> choice;
if ( choice == 1 )
{
system ("cls");
outfile.open("con");
}
else //routine allows interactive entry of external file name
{
cout << "Which drive: a, b, c, d, e, or f ? ";
cin >> drive;
strcpy(disk_file, drive);
strcat(disk_file, ":");
cout << "Enter a results file name: ";
cin >> file;
strcat(disk_file, file);
strcat(disk_file, ".dta");
outfile.open(disk_file);
}
outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
outfile << "MOREPRINTS Photo Store" << endl;
outfile << "======================" << endl;
outfile << "Customer Name " << name << endl;
outfile << "Photos In " << photos <<
outfile << "Reprints Ordered " << reprints << endl;
outfile << "total reprints " << total_photos << endl;
outfile << "Price/Reprint " << price_per << endl;
outfile << "Reprint Cost " << cost_prints << endl;
outfile << "Tax " << tax_total << endl;
outfile << "Total Cost " << price_total << endl;
outfile << "photos " << photos << endl;
outfile.close();
cout << endl << endl; // provides blank line before pause display message
system ("pause");
return 0;
} cout << "please enter your first and last name" << endl; cin >> name;
That is your problem, and this is the fix:
cout << "please enter your first and last name" << endl;
getline(cin, name); The reason it's the problem is operator>>() stops reading at whitespace. So if you type "Joe Schmoe", name will only get populated with "Joe", and " Schmoe" will be left in the stream for the next input request.
The next input request is cin >> photos, which is looking for a double value, so it will fail on "Schmoe" and put cin into an error state. Thus all of your requests for input from cin after that will fail to do anything and you'll be working with mostly uninitialized variables.
Thanks,
I tried doing it a little different since we havent gotten that far in class yet (getline). But i'm still running into the same problem. please notice changes below...
// Name: Danny Delgado
// Course: CIS-165
// Last Update: 2/21/2012
// Description: Moreprints Photo Prints
//*********************** Preprocessor Directives *********************
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
//******************************* main ********************************
int main(void)
{
double // photos entered by customer
// reprints submitted by customer
total_photos, // total photos calculated product of photos x reprints
cost_prints, // total photos x .50
tax_total, // cost of prints * .07
price_total, // cost_prints + tax_total
choice;
const double Tax_Rate = .07, // tax rate
Price_Per = .50; // price per photo
string first_name;
string last_name; // entered name
int photos,
reprints;
char drive[2],
disk_file[15],
file[9];
ofstream outfile;
// input section
system ("cls"); // clear the screen
cout << "Please Enter Your First Name" << endl;
cin >> first_name;
cout << "Please Enter Your Last Name" << endl;
cin >> last_name;
cout << "Please enter number of photos" << endl;
cin >> photos;
cout << "Please enter reprints submitted" <<endl;
cin >> reprints;
// process section
total_photos = photos * reprints;
cost_prints = total_photos * Price_Per;
tax_total = cost_prints * Tax_Rate;
price_total = cost_prints + tax_total;
// output section
system ("cls");
cout << "Output to console (1) or disk file (2): ";
cin >> choice;
if ( choice == 1 )
{
system ("cls");
outfile.open("con");
}
else //routine allows interactive entry of external file name
{
cout << "Which drive: a, b, c, d, e, or f ? ";
cin >> drive;
strcpy(disk_file, drive);
strcat(disk_file, ":");
cout << "Enter a results file name: ";
cin >> file;
strcat(disk_file, file);
strcat(disk_file, ".txt");
outfile.open(disk_file);
}
outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
outfile << "MOREPRINTS Photo Store" << endl;
outfile << "======================" << endl;
outfile << "Customer Name " << first_name << last_name <<endl;
outfile << "Photos In " << photos <<
outfile << "Reprints Ordered " << reprints << endl;
outfile << "total reprints " << total_photos << endl;
outfile << "Price/Reprint " << Price_Per << endl;
outfile << "Reprint Cost " << cost_prints << endl;
outfile << "Tax " << tax_total << endl;
outfile << "Total Cost " << price_total << endl;
outfile.close();
cout << endl << endl; // provides blank line before pause display message
system ("pause");
return 0;
} I figured I should display the output of this program to be more descriptive...
MOREPRINTS Photo Store
======================
Customer Name DannyDelgado
Photos In 56.000x22fe30Reprints Ordered 2.00
total reprints 112.00
Price/Reprint 0.50
Reprint Cost 56.00
Tax 3.92
Total Cost 59.92
photos 56.00