Need help with my C++ homework

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***************************************************************************
* DWJlab11.cpp

* COSC 1435.002
* November 26, 2013
* Purpose:
*
* Input: 
* Output: 
*	
*****************************************************************************/
#include <iostream> //header file
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
using namespace 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;
int time = 0;

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)
{
(inputFile, Popnum[x], '\n');
cout << Popnum[x] << endl;
}

cout << "Prairieville Population Growth" << endl;
cout << "Every * equals 100" << endl;
cout << "-------------------" << endl;

for (int x = 0; x + 1 < 7; x++, time +=20)
{

cout << "Year " << time << ": ";
for (int i = 0; Popnum[x] / 1000 > i; i++)
{
cout << "*";
}
}
cout << endl;
inputFile.close();

break;
} 
return 0;
}
Last edited on
Put your post in code tags lol my eyes sting...

Also what error are you getting, every bit of info helps.

I had a quick look, and I am probably wrong, but this line seems wrong to me

1
2
3
4
{
(inputFile, Popnum[x], '\n');
cout << Popnum[x] << endl;
}


x isn't defined?

I dont know I am too tired!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream> //header file
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
using namespace 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;
}
Last edited on
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: *
This is the entire program that I compiled:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream> //header file
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
using namespace std; //Required command to set the compiler environment



int main() //start of the main
{
    int choice, UserChoice = 0, Time = 1900;
    double StoreSales[5];
    double number[5];
    double Popnum[7];
    double total;

    ifstream inputFile;
    ofstream outputSalesFile;

    // prompt for imput
    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 << 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
                     << "--------------------" << endl<<endl<<endl
                     << "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[x];
                        cout << Popnum[x] << 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 = 1; Popnum[x] / 1000 > i; i++)
                    {
                        cout << "*";
                    }
                    cout << endl;
                }

                inputFile.close();
                break;
    }
    return 0;
}


Here is my output on option 3:
1
2
3
4
5
6
7
8
9
10
11
12

This is what is inside the file for People.txt
2222
6023
2323
2122
3333
9999
1212

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.
Topic archived. No new replies allowed.