Help with programming assignment

I really wanted to know how to get my program to loop to it's beginning after each switch case executes, so the user can choose another task. After they input and get the data they want, the program returns to the beginning so they can input something else(instead of re-compiling over and over again)

Here's the original assignment:

Your task is to create the following conversion table. All output should be written to a file. You will have four output files total. You MUST use the switch and case statement as part of your solution:

CIS251 CONVERSION TABLE

1. Convert from fahrenheit to celsius
2. Convert from celsius to fahrenheit
3. Convert from kilograms to pounds
4. Convert from pounds to kilograms
5. Exit

You will prompt the user to enter an option the program will execute a module based on the option entered. If option 5 is entered, processing will complete.


What I have done so far:

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
 // Jacob Ford
// Chapter 4 program exercise 1

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    ofstream taskOne;
    ofstream taskTwo;
    ofstream taskThree;
    ofstream taskFour;
    int task;
    double farenheit;
    double celsius;
    double kilograms;
    double pounds;


    cout << "___CIS251 COVERSION TABLE PROGRAM___" << endl;
    cout << "Select a task to perform(1-5):" << endl;
	cout << "1. Convert from farenheit to celsius." << endl;
	cout << "2. Convert from celsius to farenheit." << endl;
	cout << "3. Convert from kilograms to pounds." << endl;
	cout << "4. Convert from pounds to kilograms." << endl;
	cout << "5. EXIT." << endl;
	cin >> task;

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

	switch(task)
	{
    case 0:
    case 1:
        cout << "This task converts from farenheit to celsius." << endl;
        cout << "Enter degrees farenheit:" << endl;
        cin >> farenheit;
        celsius = (farenheit - 32) / 1.8;
        taskOne.open("farenheitToCelsius.out");
        taskOne << "After converting, your temperature in Celsius is:"
                << celsius << endl;
        cout << "After converting, your temperature in Celsius is:"
             << celsius << endl;
             break;

    case 2:
        cout << "This task converts from celsius to farenheit." << endl;
        cout << "Enter degrees celsius:" << endl;
        cin >> celsius;
        farenheit = (celsius * 1.8) + 32;
        taskTwo.open("celsiusToFarenheit.out");
        taskTwo << "After converting, your temperature in Farenheit is:"
                << farenheit << endl;
        cout << "After converting, your temperature in Farenheit is:"
             << farenheit << endl;
             break;

      case 3:
        cout << "This task converts from kilograms to pounds." << endl;
        cout << "Enter weight in kilograms:" << endl;
        cin >> kilograms;
        pounds = kilograms / 0.453592;
        taskThree.open("kilogramsToPounds.out");
        taskThree << "After converting, your weight in Pounds is:"
                << pounds << endl;
        cout << "After converting, your weight in Pounds is:"
             << pounds << endl;
             break;

           case 4:
        cout << "This task converts from pounds to kilograms." << endl;
        cout << "Enter weight in pounds:" << endl;
        cin >> pounds;
        kilograms = pounds * 0.453592;
        taskFour.open("poundsToKilograms.out");
        taskFour << "After converting, your weight in Kilograms is:"
                << kilograms << endl;
        cout << "After converting, your weight in Kilograms is:"
             << kilograms << endl;
        break;

           case 5:
        cout << "To exit, press ENTER." << endl;
        break;

           default:
            cout << "You've chosen an invalid task! 1 through 5 only!" << endl;
            
	}

    return 0;
}
Last edited on
You nearly have it, have a read of this :

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


Just need to add the bool controlled while loop.

Also the code would be tidier, if each case called a function, rather than having a big long winded switch. The code which shows the menu should be in it's own function as well.

Declare the functions before main(), then place the definitions of them after main()

And avoid the using namespace std; - it brings the whole of the std namespace into the global one, which can cause naming conflicts - which is what we are trying avoid. Do you realise there are a number commonly used words in std:: like std::left, std::right, std::distance to name just a few. So if you had a variable or function called distance, the compiler would throw out all kinds of messages, because std::distance means a very different thing.

So you can put std:: before each std thing (a lot of people do this), or you can put these at the top of your file :

1
2
3
4
using std::cout;
using std::cin;
using std::endl;
using std::string;


Then use them without qualification after that.

Ideally, one should always put their code in a namespace of it's own. You can read about in the reference section top left of this page.

Hope all goes well.
@TheIdeasMan,

Preciate all the pointers! I'll have to read those reference sections thoroughly I didn't know all that about using namespace std;

thanks for the links
Topic archived. No new replies allowed.