Writing to an outFile

I am having trouble writing my program to an outFile. When I try it only lets my program execute one time, instead of multiple times. I need basic beginners instruction.....Help??!!


#include <iostream>
#include <iomanip>

using namespace std;

void menu();
void car(double, double, double, double);
void air(double, double, double, double);
void hel(double, double, double, double);
void hyd(double, double, double, double);

int main()
{

cout << showpoint << fixed << setprecision(1);

int option;
double time;
double distancemeters = 0;
double distancemiles = 0;
const double meters_mile = 1609;

cout << "Distance of Sound in Gases" << endl;

do
{
menu();
cin >> option;
cout << endl;

switch (option)
{
case 1:
{
cout << "Enter the number of seconds: " << endl;
cin >> time;

while (time < 0 || time > 30)
{
cout << "The number must be between 0 and 30." << endl;
cout << "Enter the number of seconds: " << endl;
cin >> time;

}

car(time, distancemeters, distancemiles, meters_mile);
break;
}
case 2:
{
cout << "Enter the number of seconds: " << endl;
cin >> time;

while (time < 0 || time > 30)
{
cout << "The number must be between 0 and 30.";
cout << "Enter the number of seconds: " << endl;
cin >> time;

}
air(time, distancemeters, distancemiles, meters_mile);
break;
}
case 3:
{
cout << "Enter the number of seconds: " << endl;
cin >> time;

while (time < 0 || time > 30)
{
cout << "The number must be between 0 and 30." << endl;
cout << "Enter the number of seconds: " << endl;
cin >> time;
}
hel(time, distancemeters, distancemiles, meters_mile);
break;
}
case 4:
{
cout << "Enter the number of seconds: " << endl;
cin >> time;

while (time < 0 || time > 30)
{
cout << "The number must be between 0 and 30." << endl;
cout << "Enter the number of seconds: " << endl;
cin >> time;
}

hyd(time, distancemeters, distancemiles, meters_mile);
break;
}
case 5:
break;
default:
cout << "You must select 1, 2, 3, or 4!" << endl;
break;
}
}
while (option != 5);
cout << "That's All Folks...." << endl;

return 0;
}
void menu()
{
cout << "1. Distance of Carbon Dioxide" << endl;
cout << "2. Distance of Air" << endl;
cout << "3. Distance of Helium" << endl;
cout << "4. Distance of Hydrogen" << endl;
cout << "5. Quit" << endl;
cout << "Enter your choice (1-5): " << endl;

}
void car(double time, double distancemeters, double distancemiles, double meters_mile)
{
const double carbonspeed = 258.0;
distancemeters = carbonspeed * time;
distancemiles = distancemeters / meters_mile;

cout << "The distance traveled in Carbon is " << distancemeters << " meters or " << distancemiles << " miles.\n" << endl;

if (distancemiles < 1)
cout << "The sound source is very close!\n" << endl;

else if (distancemiles < 15)
cout << "The sound source is moderately close!\n" << endl;

}

void air(double time, double distancemeters, double distancemiles, double meters_mile)

{
const double airspeed = 331.5;
distancemeters = airspeed * time;
distancemiles = distancemeters / meters_mile;

cout << "The distance traveled in Air is " << distancemeters << " meters or " << distancemiles << " miles.\n" << endl;

if (distancemiles < 1)
cout << "The sound source is very close!\n" << endl;
else if (distancemiles < 15)
cout << "The sound source is moderately close!\n" << endl;
}
void hel(double time, double distancemeters, double distancemiles, double meters_mile)
{
const double heliumspeed = 972.0;
distancemeters = heliumspeed * time;
distancemiles = distancemeters / meters_mile;

cout << "The distance traveled in Helium is " << distancemeters << " meters or " << distancemiles << " miles.\n" << endl;

if (distancemiles < 1)
cout << "The sound source is very close!\n" << endl;
else if (distancemiles < 15)
cout << "The sound sorce is moderately close!\n" << endl;

}

void hyd(double time, double distancemeters, double distancemiles, double meters_mile)

{
const double hydrogenspeed = 1270.0;
distancemeters = hydrogenspeed * time;
distancemiles = distancemeters / meters_mile;

cout << "The distance traveled in Hydrogen is " << distancemeters << " meters or " << distancemiles << " miles.\n" << endl;

if (distancemiles < 1)
cout << "The sound source is very close!\n" << endl;
else if (distancemiles < 15)
cout << "The sound source is moderately close!\n" << endl;
}
I'm not sure about the reference to an outFile, there's no trace of one in the posted code.

The most noticeable thing here (apart from the missing [code]your code here[/code] tags) is the amount of repeated code.

The program can be simplified by putting all the repeated code in one place, and keeping just the parts which are different in the switch-case.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void menu();
double getSeconds();
void calculate(double time, double speed, string gasName);

int main()
{
    cout << showpoint << fixed << setprecision(1);

    int option = 0;
    double time;

    cout << "Distance of Sound in Gases" << endl;

    do
    {
        menu();
        cin >> option;
        cout << endl;
        
        if (option > 0 && option < 5)
            time = getSeconds();
        
        switch (option)
        {
            case 1:
                calculate(time, 258.0, "Carbon Dioxide");
                break;

            case 2:
                calculate(time, 331.5, "Air");
                break;
          
            case 3:
                calculate(time, 972.0, "Helium");
                break;
               
            case 4:
                calculate(time, 1270.0, "Hydrogen");
                break;
              
            case 5:
                break;
                
            default:
                cout << "You must select 1, 2, 3, or 4!" << endl;
                break;
        }
    } while (option != 5);
    
    cout << "That's All Folks...." << endl;

    return 0;
}

void menu()
{
    cout << "1. Distance of Carbon Dioxide" << endl;
    cout << "2. Distance of Air" << endl;
    cout << "3. Distance of Helium" << endl;
    cout << "4. Distance of Hydrogen" << endl;
    cout << "5. Quit" << endl;
    cout << "Enter your choice (1-5): " << endl;
}

double getSeconds()
{
    double time;
    
    cout << "Enter the number of seconds: " << endl;
    cin >> time;

    while (time < 0 || time > 30)
    {
        cout << "The number must be between 0 and 30." << endl;
        cout << "Enter the number of seconds: " << endl;
        cin >> time;
    }
    
    return time;
}

void calculate(double time, double speed, string gasName)
{
    static const double meters_mile = 1609;
    double distancemeters = speed * time;
    double distancemiles  = distancemeters / meters_mile;

    cout << "The distance traveled in "  << gasName 
         << " is " << distancemeters     << " meters or " 
         << distancemiles << " miles.\n" << endl;

    if (distancemiles < 1)
        cout << "The sound source is very close!\n" << endl;
    else if (distancemiles < 15)
        cout << "The sound source is moderately close!\n" << endl;
}

Last edited on
Thanks for your information, but I still have to get the program to write to an output file...
Ok. However, you haven't explained what information you want to be put into the output file, and under what circumstances. In order to provide any useful help you need to be clearer about what it is you want the program to achieve.

The original post offered this detail:
When I try it only lets my program execute one time, instead of multiple times.
that suggests the actual problem was getting the while-loop to behave correctly.
To be fair, I guess I was distracted by the code which was posted. If the question had been asked with no accompanying code at all, then a good starting point would be this section of the tutorial pages:
http://www.cplusplus.com/doc/tutorial/files/

Of course, putting theory into practice isn't always trouble-free. I suggest you combine some of the ideas from that tutorial with some of your existing code, put them together and if you have problems, post the code showing how far you got - and describing any problems there are.
When you run this program, ALL of the results which execute correctly on the console needs to be written out. So to make it simple for every cout to console it needs to be written to an output file. I would put it in the code but i do not understand how. My professor does not want a screen shot he wants the results put in to an output file and printed to be turned in. Ex: when I chose 1-5 as prompted it executes correctly on console, but when I add output commands under my couts it will only execute 1 choice and my professor wants to see them ALL execute on my output file.
Last edited on
I got it figured out....thanks!!
Topic archived. No new replies allowed.