I wrote this on notepad++ and I cannot get it to run on Dev++. The code is made up of a switch with three parts for a menu. The first two menu items compile at my school's SSH, but the second menu item should be writing the output to a .txt file and its not. It should pull information from a file that is in my folder with the source code as well.
CORRECTION: I got the second part now case 3 is giving me trouble can you guys help me figure it out. Here's the updated code:
#include <iostream> //header file
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
usingnamespace std; //Required command to set the compiler environment
int main() //start of the main
{
int choice;
double StoreSales[5];
double number[5];
double Popnum[7];
double total;
ifstream inputFile;
ofstream outputSalesFile;
int UserChoice = 0, Time = 1900;
cout << "Choose from the menu, which part of the program you wish to run. Type 1, 2, or 3 :" << endl;
cout << "1. Sales Bar Chart" << endl;
cout << "2. Sales Bar Chart with Files" << endl;
cout << "3. Population Bar Chart" << endl;
cout << endl;
cin >> choice;
cout << "You have chosen: " << choice << "!" << endl;
cout << endl;
switch(choice)
{
case 1:
cout << "Sales Bar Chart" << endl;
cout << "---------------" << endl;
for (int x = 0; x < 5; x++)
{
cout << "Please enter the total sales at the store today " << x + 1 << ": ";
cin >> StoreSales[x];
total = (StoreSales[x] + total);
}
cout << "Sales Bar Chart" << endl;
cout << "Every * equals $100" << endl;
cout << "-------------------" << endl;
for (int x = 0; x < 5; x++)
{
cout << "Store: " << x << ":";
for (int i = 0; StoreSales[x] / 100 > i; i++)
{
cout << "*";
}
cout << endl;
}
break;
case 2:
cout << "Sale Bar Chart with Files" << endl;
cout << "-------------------------" << endl;
cout << endl;
cout << "This is what is inside the file" << endl;
cout << endl;
inputFile.open("Sales.txt");
if (inputFile)
{
for (int x = 0; x < 5; x++)
{
(inputFile >> number[x], '\n');
cout << number[x] << endl;
}
inputFile.close();
outputSalesFile.open("OutputSales.txt");
outputSalesFile << "Sales Bar Chart\n\r" << endl;
outputSalesFile << "[Every * equals 100]\n\r" << endl;
outputSalesFile << "-------------------------\n\r" << endl;
for (int x = 0; x < 5; x++)
{
outputSalesFile << "Store " << x + 1 << ":";
for (int i = 0; number[x] / 100 > i; i++)
{
outputSalesFile << "*";
}
outputSalesFile << endl;
}
}
outputSalesFile.close();
cout << endl;
cout << "The result has been output to a .txt file called OutputSale.txt" << endl;
break;
case 3:
cout << "Population Bar Chart" << endl;
cout << "--------------------" << endl;
cout << endl;
cout << endl;
cout << "This is what is inside the file for People.txt" << endl;
inputFile.open("People.txt");
if (inputFile)
{
for (int x = 0; x < 7; x++)
(inputFile, Popnum[7]);
cout << Popnum[7] << endl;
}
cout << "Prairieville Population Growth" << endl;
cout << "Every * equals 1000" << endl;
cout << "-------------------" << endl;
for (int x = 0; x + 1 < 7; x++, Time +=20)
{
cout << "Year " << Time << ": ";
for (int i = 0; Popnum[i] / 1000 > i; i++)
{
cout << "*";
}
cout << endl;
}
inputFile.close();
break;
}
return 0;
}
this is case 3 in my switch and the output I'm getting from it is:
Choose from the menu, which part of the program you wish to run. Type 1, 2, or 3 :
1. Sales Bar Chart
2. Sales Bar Chart with Files
3. Population Bar Chart
3
You have chosen: 3!
Population Bar Chart
--------------------
This is what is inside the file for People.txt
4.88906e-270
Prairieville Population Growth
Every * equals 1000
-------------------
Year 1900: *
Year 1920: *
Year 1940: *
Year 1960: *
Year 1980: *
Year 2000: *
I think the problem you are referring to is the embedded loop. Consider the logic:
1 2 3 4 5 6 7 8 9
for (int x = 0; x + 1 < 7; x++, Time +=20)
{
cout << "Year " << Time << ": ";
for (int i = 0; Popnum[i] / 1000 > i; i++)
{
cout << "*";
}
cout << endl;
}
You are checking Popnum[i] against the variable i. Is this your intent? I think you are actually trying to compare the CURRENT Popnum or x value. In your example the for loop is going to compare different indexes for Popnum with every iteration. Also, you are printing one more star than needed, so I started the loop with 1 rather than 0.
1 2 3 4 5 6 7 8 9
for (int x = 0; x + 1 < 7; x++, Time +=20)
{
cout << "Year " << Time << ": ";
for (int i = 1; Popnum[x] / 1000 > i; i++)
{
cout << "*";
}
cout << endl;
}
What I'm trying to is have the program open a .txt file called People.txt and then take the information from it and display it in an output that arranges * for every 1000 people per year.
but the part where it puts 4.88906e-270 is wrong it shouldn't be putting that. I don't know what to do there.
I made the swap and this is what came out as output:
This is what is inside the file for People.txt
4.88906e-270
Prairieville Population Growth
Every * equals 1000
-------------------
Year 1900: *
Year 1920:
Year 1940: *
Year 1960: *
Year 1980: *
Year 2000: *
THank you so much, that really helped. My output is working, I will further study and practice how you did your code so I won't run into this problem again in the future. Thank you again.
Please pay attention to where the values are coming from. You are pulling the values from People.txt so they have to already be there. Perhaps that is what the problem was other than the loop? I loaded my own values, but am not entirely sure if that was your intent. If this is an assignment be sure to double check the directions. If this should be done in another part of the program be sure to initialize the values at start with all zeros to be sure you are not reading junk. Any other questions just ask.......love learning and helping here.