I am doing C++ program which reads infinite while loop,
After letting user enter the desired month and year.
The loop become infinite and only output "AAAAA...."
Textfile(input)
1 9 2017|0112233445|20.00 5.00 2 9 2017|0123456789|6.00 3.00
3 10 2017|0135792468|30.00 1.00 4 10 2017|0166778899|7.00
10.00 4 10 2017|0177001228|40.00 4.00 5 10 2017|0184040626|8.00 2.00 6 10 2017|0134567892|50.00 6.00 7
10 2017|0145678910|9.00 7.00
I tried using while(inS >> Day>>Month >> Year >> ch >>hpNO >> ch >> c_Charges >> sms_charges )
but nothing seems to be worked .Any suggestions to solve this problem?Thanks in advance.
void monthlyreport(char manager[])
{
SYSTEMTIME T;
GetSystemTime(&T);
int month, year; //userinput
char ch;//store delimitor
int count = 0;
double Totalcallcharges = 0;
double Totalsmscharges = 0;
double Totalcharges = 0;
int Day,Month, Year;//for textfile reading
char hpNO[11];
double c_Charges =0;
double sms_charges =0;
string Months[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
system("cls");
cout << "You choose : MONTHLY CHARGES REPORT ...";
cout << "\n\n Which month/year (mm yyyy)? ";
cin >> month;
cin >> year;
ifstream inS;
inS.open("Transaction.txt");
/*while (inS >> Day)
{
while (inS >> Month)
{
while (inS >> Year)
{
while (inS >> ch)
{
while (inS >> hpNO)
{
while (inS >> ch)
{
while (inS >> c_Charges)
{
while (inS >> sms_charges)
{
if (month == Month && year == Year)
{
Totalcallcharges += c_Charges;
}
if (month == Month && year == Year)
{
Totalsmscharges += sms_charges;
}
count++;
}
}
}
}
}
}
}
}
*/
while (!inS.eof()) correct answer
{
inS >> tranS.day >> tranS.month >> tranS.year >> ch;
inS.getline(tranS.phoneNO, 12, '|');
inS >> tranS.call_charges >> tranS.sms_charges;
transactions(&head, tranS);
if (tranS.month == month &&tranS.year == year)
{
tranS.Total_call_charges += tranS.call_charges;
tranS.Total_sms_charges += tranS.sms_charges;
}
count++;
}
Totalcharges = Totalcallcharges + Totalsmscharges;
inS.close();
system("cls");
cout << Month << year;
cout << endl << c_Charges;
cout << setw(50) << setfill('=') << " " << endl << endl;
cout << " T A R U C M O B I L E S D N B H D" << endl << endl;
cout << setfill(' ') << setw(30) << "Monthly Report - " << Months[month - 1] << " " << year << endl;
cout << "\nTotal Number of TARUCMOBILE users = " << count;
cout << "\n\nTotal Charges from Calls = RM" << setw(8) << setfill(' ') << Totalcallcharges << setprecision(2);
cout << "\nTotal Charges from SMS's = RM" << setw(8) << setfill(' ') << Totalsmscharges << setprecision(2);
cout << "\n" << setfill(' ') << setw(27) << " " << right << setw(12) << setfill('-') << "-" << endl;
cout << "TOTAL CHARGES (POSTPAID) = RM" << setw(8) << setfill(' ') << Totalcharges << setprecision(2);
cout << "\n" << setw(27) << setfill(' ') << " " << setw(12) << setfill('=') << "=" << endl;
cout << "*** E N D O F R E P O R T *** " << endl << endl;
cout << "Generated by" << endl;
cout << "______________" << endl;
cout << manager << "(Manager)" << endl;
cout << T.wDay << " " << Months[T.wMonth - 1] << " " << T.wYear << endl;
cout << endl << setw(50) << setfill('=') << " " << endl;
system("pause");
}
Try
while(inS >> Day)
{
while(inS >> Month)
{
while(inS >> Year)
{
while(inS >>ch)
{
.
.
.
}
Then do whatever you want to inside the innermost while loop.
Related
I am trying to fix my output using setw(4).I have tried different a range of number between 1 to 10 but none get me the desired output.
void Students(int& size_public)
{
cout << "Input the number of information to be entered: ";
cin >> size_public;
cout << endl << endl;
// Reseting the screen
system("cls");
for (int i = 0; i < size_public; i++)
{
cout << "________________________Enter the following information for the data to be stored________________________\n" << endl;
cout << "Name: ";
cin >> employees[i].name_public;
cout << "Age: ";
cin >> employees[i].age_public;
cout << "ID.No: ";
cin >> employees[i].ID_No_publice;
cout << endl;
system("cls");
}
}
void Students(Employee employees[arraysize], int& size_public)
{
for (int i = 0; i < size_public; i++)
{
if (i == 0)
{
cout << setw(10) << "Name\t" << "Age\t" << "ID.No" << endl << endl;
}
cout << setw(10) << employees[i].name_public << '\t'
<< employees[i].age_public << '\t'
<< employees[i].ID_No_publice << endl;
}
}
};
Later in the program it is called
if (selection == 2)
{
student.Students(size);
cout << "1. Leave\n";
cout << "2. Showinformation\n";
cout << "\n________________________________________________\n";
cout << "Enter your choice: ";
cin >> choice;
cout << "\n________________________________________________\n";
if (choice == 1)
{
cout << endl;
}
else if (choice == 2)
{
student.Students(student.employees, size);
}
else if (choice != 1 && choice != 2)
{
cout << "choice is not found\n";
}
}
This is what I am talking about
Name Age ID.No
fge 33 345674
sdfgfd 34 23
dfghjkjhg 354 54345
Space behind the names is what I am trying to avoid
This is the output i receive when i use setw(10)
and this is what happen when the range of input changes
Name Age ID.No
afjghbslkk;jfl 2 3
fg 3 5.67654e+06
3 34543 543
if you look at fg in name it has a space behind it and I am trying to avoid it
My desired output is something like
Name age ID.No
dfghsgffgdf 44553 4564564
ajkghjkgh 444 465454
ff 4 46
I do not know if it is possible but if I am missing any thing just let me know and i will update the code.
Example
if (i == 0)
cout << left << setw(10) << "Name" << setw(10) << "Age" << setw(10) << "ID.No" << endl << endl;
cout << left << setw(10) << employees[i].name_public
<< setw(10) << employees[i].age_public
<< setw(10) << employees[i].ID_No_publice << endl;
See if this is what you need.
I'm doing an assignment for class where I need to output my program to look something like this. Right now I'm stuck on how to output the years like that.
I've added a counter for years and asked for current year as an input?
Can someone lend me a hand?
Town A Town B
8.5% 0.3%
Year 5000 8000
-------------------------------------------
2016 5425 8024
2017 5886 8048
2018 6386 8072
2019 6929 8096
2020 7518 8121
2021 8157 8145
cout << "What year is it? ";
cin >> curYear;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
years++;
curYear++;
townA = finalA;
townB = finalB;
}
cout << "\n\n" << endl;
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(14) << growthB << "%" << endl;
cout << setw(4) << townA << setw(16) << townB << endl;
cout << "Year" << endl;
cout << curYear << endl;
cout << "--------------------------------------------" << endl;
cout << endl;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
return 0;
}
I'm a beginner who has some questions relating to my homework for school.
With my current code I'm stuck in a neverending loop which I'm assuming is because my condition is never actually met (Pop A being greater than Pop B) and I'm unsure how to move on. I'm also not sure how to properly increment/calculate the years needed for Town A to surpass Town B but this is what I've got so far.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int townA;
int townB;
double growthA;
double growthB;
double finalA;
double finalB;
int years = 0;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
}
cout << "\n\n" << endl; // Teacher required us to output it to the screen incase anyone is wondering why I have this block
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(10) << growthB << "%" << endl;
cout << setw(3) << townA << setw(7) << townB << endl;
cout << "Year" << endl;
cout << "--------------------------------------------" << endl;
return 0;
}
You are not updating the values of townA and townB in the loop. Hence, you never get out of the loop.
Also, you need to increment years in the loop. Use:
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
// Update the values of the key variables.
++years;
townA = finalA;
townB = finalB;
}
// This needs to be moved from the loop.
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)
I'm trying to sort alphabetically by team for my program, but not having any luck. If there is any hints or advice out there I would appreciate it. Below is the program I have minus the data input. Basically I just want to know how I would go about specifically sorting only by team in alphabetical order.
nflrecievers data[100];
ifstream fin;
fin.open("data.txt");
ofstream fout;
fout.open("validationReport.txt");
int i = 0;
while(!fin.eof())
{
fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
i = i + 1;
}
int a;
int b;
cout << " Select NFL Receivers Statistics. Input 1-4 " << endl;
cout << " 1) Receivers with 25+ Rec and 300+ Yards. " << endl;
cout << " 2) Recievers with 3+ TDs and 3+ Rec over 20 Yards. " << endl;
cout << " 3) Recievers with 100+ Yards per game and 15+ First Downs. " << endl;
cout << " 4) Veiw Total Recievers Statistics. " << endl;
cin >> a;
int c = 0;
if (a==1)
{
cout << " Receivers with 25+ Rec and 300+ Yards. " << endl;
while( c < i-1)
{
if(data[c].rec > 25 && data[c].yards > 300)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " Rec: " << data[c].rec << " Yards: " << data[c].yards << endl;
cout << endl;
}
c++;
}
}
else if(a==2)
{
cout << " Recievers with 3+ TDs and 3+ Receptions past 20 Yards. " << endl;
while( c < i-1)
{
if(data[c].tds > 3 && data[c].recpasttwenty_yrds > 3)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " TDs: " << data[c].tds << " Receptions past 20 Yards: " << data[c].recpasttwenty_yrds << endl;
cout << endl;
}
c++;
}
}
else if(a==3)
{
cout << " Recievers who average over 100+ yards per game and 15+ First Downs. " << endl;
while( c < i-1)
{
if(data[c].yrds_pergame > 100 && data[c].first_dwns > 15)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " Average Yards per game: " << data[c].yrds_pergame << " First Downs: " << data[c].first_dwns << endl;
cout << endl;
}
c++;
}
}
else if(a==4)
{
cout << " Select a Reciever: " << endl;
while( c < i-1)
{
cout << c << ") " << data[c].fname << " " << data[c].lname << endl;
c++;
}
cout << " Total NFL Receivers Statistics. " << endl;
cin >> b;
cout << data[b].fname << " " << data[b].lname << endl;
cout << " Team: " << data[b].team << endl;
cout << " Receptions: " << data[b].rec << endl;
cout << " Yards: " << data[b].yards << endl;
cout << " Average Yards Per Catch: " << data[b].avgyrds_percatch << endl;
cout << " Longest Reception: " << data[b].longest_rec << endl;
cout << " Receptions over 20 Yards: " << data[b].recpasttwenty_yrds << endl;
cout << " Yards per game " << data[b].yrds_pergame << endl;
cout << " Fumbles: " << data[b].fumbles << endl;
cout << " Average Yards After Catch " << data[b].yac << endl;
cout << " Total First Downs: " << data[b].first_dwns << endl;
}
return 0;
}
std::sort(std::begin(data), std::end(data),
[](const nflrecievers& a, const nflrecievers& b) { return a.team < b.team; });
I would use std::sort
bool compare_teams(const nflrecievers &a, const nflrecievers &b) {
return a.team < b.team;
}
int i = 0;
while(!fin.eof())
{
fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
i = i + 1;
}
std::sort(data, data+i, compare_teams);
for (int i=0;i<c-1;i++)
for (int j=0;j<c-1;j++)
if (data[j].team>data[j+1].team)
{
nflrecievers temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
another solution:
int compare(const void *v1, const void *v2)
{
nflrecievers p1 = *(nflrecievers *)v1, p2 = *(nflrecievers *)v2;
return strcmp(p1.team,p2.team);
}
qsort(data,c,sizeof(data),compare);