While or do-while loop??

What should I use to make this code. Should I use do-while loops? or just while loops? Or nested if and else statements?
It also tells me that 3x10^(19) is too large of an interger when I enter it.
What do I do for that?

I need help. My professor assigned this.

"Write (also compile and execute) a complete Visual C++ program that selects, via keyboard, wavelength or frequency, prompts the user to enter a value, and then displays the corresponding electromagnetic region, using the data listed in the table above.
YOUR PROGRAM MUST PERFORM THE FOLLOWING TASKS:

1) Prompt the user to choose what type of input (wavelength or frequency) he/she wants to enter. Use the data type character ‘f’ or ‘F’ to select frequency and ‘w’ or ‘W’ to select wavelength. If the character entered is different from ‘f’, ‘F’, ‘w’ or ‘W’ your program MUST display a message stating for instance “invalid option” and then allow the user to reenter the character WITHOUT THE NEED to rerun the program.

2) Prompt the user to enter a number (data type double) which will be used to determine the electromagnetic region. Verify if the number entered (either frequency or wavelength) is positive. If the number entered is negative (or zero) your program MUST display a message stating for instance “number must be positive” and then allow the user to reenter the number WITHOUT THE NEED to rerun the program.

3) In the case of wavelength selection your program MUST prompt the user to enter the wavelength value either in meters, centimeters or millimeters (recall: 1m≡100 cm and 1mm≡0.1 cm) and then perform the necessary unit conversion using the table above which wavelength units are in centimeters. If the unit selection entered is not valid your program MUST display a message stating for instance “invalid option” and then allow the user to reenter the unit WITHOUT THE NEED to rerun the program.

4. Your program MUST also allow the user to START OVER as often he/she wishes,
WITHOUT THE NEED to rerun the program."

He has given us the table as far as to what each wavelength and frequency is equal to. I got the first part down correctly but I can't get past the second part. No matter what number I input the out put is always "wavelength" and "The Electromagnetic Region for the wavelength is Radio"


this is the table:
Electromagnetic Region --- Wavelength (cm) --- Frequency
Radio --- >10 --- <3x10^9
Microwave --- 0.01 to <10 --- 3x10^9 to 3x10^12
Infrared --- 7x10^-5 to < 0.01 --- >3x10^12 to 4.3x10^14
Visible --- 4 x 10^-5 to <7 x 10^-5 --- >4.3x10^14 to 7.5x10^17
Ultraviolet --- 1x10^-7 to <4 x 10^-5 --- >7.5x10^14 to 3x10^17
X-Rays --- 1x10^-9 to <1x10^-7 --- >3x10^17 to 3x10^19
Gamma Rays --- <10^-9 --- >3x10^19



This is what I have so far.
Thanks for any help.

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
#include <iostream>
using namespace std;

int main()
{
	char x;
	do
	{
		cout << "Please indicate wether you want to input the wavelength or frequency. " << endl;
		cout << "Enter W or w for wavelength and F or f for frequency. ";
		cin >> x;
		if ((x != 'W') && (x != 'w') && (x != 'F') && (x != 'f'))
			cout << "Invalid Entry. Please try again. " << endl;
		else
			cout << x << endl;
	} while ((x != 'W') && (x != 'w') && (x != 'F') && (x != 'f'));

	char wl;
	do{
		cout << "You have selected Wavelength. " << endl;
		cout << "Plese Enter a number greater than zero. " << endl;
		cin >> wl;

		while (wl <= 0)
			cout << "Invalid Entry. Enter a number greater than zero. " << endl;

		while (wl > 10)
			cout << "The Electromagnetic Region for the wavelength is Radio. " << endl;

		while ((wl > .01) && (wl<10))
			cout << "The Electromagnetic Region for the wavelength is Microwave. " << endl;

		while ((wl>.00007) && (wl < .01));
		cout << "The Electromagnetic Region for the wavelength is Infrared. " << endl;

		while ((wl>.00004) && (wl < .00007));
		cout << "The Electromagnetic Region for the wavelength is Visible. " << endl;

		while ((wl>.0000001) && (wl < .00004));
		cout << "The Electromagnetic Region for the wavelength is Ultraviolet. " << endl;

		while ((wl>.000000001) && (wl < .0000001));
		cout << "The Electromagnetic Region for the wavelength is X-Rays. " << endl;

		while (wl < .000000001)
			cout << "The Electromagnetic Region for the wavelength is Gamma Rays. " << endl;
	} while ((x == 'w') && (x == 'W'));

		 char fr;
		 do{
			 cout << "You have selected Frequency. " << endl;
			 cout << "Plese Enter a number greater than zero. " << endl;
			 cin >> fr;
			 while (fr <= 0)
				 cout << "Invalid Entry. Enter a number greater than zero. " << endl;
			 while (fr < 3000000000)
				 cout << "The Electromagnetic Region for the frequency is Radio. " << endl;
			 while ((fr > 3000000000) && (fr<3000000000000))
				 cout << "The Electromagnetic Region for the frequency is Microwave. " << endl;
			 while ((fr>3000000000000) && (fr < 43000000000000));
			 cout << "The Electromagnetic Region for the frequency is Infrared. " << endl;
			 while ((fr>430000000000000) && (fr < 750000000000000));
			 cout << "The Electromagnetic Region for the frequency is Visible. " << endl;
			 while ((fr>750000000000000) && (fr < 300000000000000000));
			 cout << "The Electromagnetic Region for the frequency is Ultra-Violet. " << endl;
			 while ((fr>300000000000000000)&& (fr < 30000000000000000000));
			 cout << "The Electromagnetic Region for the frequency is X-rays. " << endl;
			 while ((fr>300000000000000000));
			 cout << "The Electromagnetic Region for the frequency is Gamma-Rays. " << endl;
		 } while ((x == 'f') && (x == 'F'));

	return 0;
}
Last edited on
Sorry for the caps. I just copied from the assignment document.
The reason why you're always getting "wavelenght" is because you're using a do while loop. A do-while loop will always be executed at least once because the condition is only checked at the end.

Also, you should consider replacing most of your loops with if/else statements. You only really need loops to "start over" if an input is invalid. Everything else is just determining what to output, and if/else statements would be better for that.

Another thing: Your do-while loop conditions are incorrect, or at least the way you typed them. Take line 47 for example. X cannot have value of 'w' and 'W' at the same time, so the condition is never met.
Topic archived. No new replies allowed.