0% found this document useful (0 votes)
5 views3 pages

CSC01A1 2024 P04 Solution

Uploaded by

Thabo Innocent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

CSC01A1 2024 P04 Solution

Uploaded by

Thabo Innocent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 /*

2 * CSC01A1 2024 P03 Solution


3 * Mr SP Sithungu
4 * 15 March 2024
5 */
6 #include <iostream>
7 #include <cctype>
8 #include <string>
9
10 #ifdef _WIN32
11 #define CLEAR_COMMAND "cls"
12 #else
13 #define CLEAR_COMMAND "clear"
14 #endif
15
16 using namespace std;
17
18 int main()
19 {
20 const int SUCCESSFUL_TERMINATION = 0;
21
22 bool blnContinue = true;
23 char chOption = '\0';
24 do
25 {
26 system("cls");
27 cout << "Option A: Output Geometric Sequence of n Terms." << endl
28 << "Option B: Counting Sequence." << endl
29 << "Option C: Output a given string in reverse order." << endl
30 << "Option X: Exit." << endl;
31 cin >> chOption;
32 switch(chOption)
33 {
34 case 'a':
35 case 'A':
36 {
37 cout << "Enter the number of terms: ";
38 int n = 0;
39 cin >> n;
40 while(cin.fail() || n <= 1)
41 {
42 cin.clear();
43 cin.ignore(100, '\n');
44 cerr << "Error: You must enter an integer greater than 1: ";
45 cin >> n;
46 }
47 cout << "Enter the common difference: ";
48 int d = 0;
49 cin >> d;
50 while(cin.fail() || d <= 1)
51 {
52 cin.clear();
53 cin.ignore(100, '\n');
54 cerr << "Error: You must enter an integer greater than 1: ";
55 cin >> d;
56 }
57 int intPrevTerm = 2; // Students are allowed to ask the user for the starting term.
58 cout << intPrevTerm << ", ";
59 for(int i = 0; i < n; i++){
60 intPrevTerm *= d;
61 cout << intPrevTerm << ", ";
62 }
63 break;
64 }
65 case 'b':
66 case 'B':
67 {
68 int intStart = 0;
69 int intStop = 0;
70 int intStep = 0;
71 cout << "Enter a starting value: ";
72 cin >> intStart;
73 while(cin.fail() || intStart <= 1)
74 {
75 cin.clear();
76 cin.ignore(100, '\n');
77 cerr << "Error: You must enter an integer greater than 1: ";
78 cin >> intStart;
79 }
80
81 cout << "Enter a stopping value: ";
82 cin >> intStop;
83 while(cin.fail() || intStop <= 1)
84 {
85 cin.clear();
86 cin.ignore(100, '\n');
87 cerr << "Error: You must enter an integer greater than 1: ";
88 cin >> intStop;
89 }
90
91 cout << "Enter a step value: ";
92 cin >> intStep;
93 while(cin.fail() || intStep <= 1)
94 {
95 cin.clear();
96 cin.ignore(100, '\n');
97 cerr << "Error: You must enter an integer greater than 1: ";
98 cin >> intStep;
99 }
100
101 for(int i = intStart; i <= intStop; i += intStep)
102 {
103 cout << i << " ";
104 }
105 break;
106 }
107 case 'c':
108 case 'C':
109 {
110 string strCharSequence = "";
111 cout << "Enter a character sequence: ";
112 cin.ignore();
113 getline(cin, strCharSequence);
114 string strReverseSeq = "";
115 for(char c : strCharSequence){
116 strReverseSeq = c + strReverseSeq;
117 }
118 cout << strReverseSeq << endl;
119 break;
120 }
121 case 'x':
122 case 'X':
123 {
124 // Exit the program.
125 cout << "Bye." << endl;
126 blnContinue = false;
127 }
128 default:
129 cout << "Invalid choice. Please enter a valid option.\n";
130 break;
131 }
132 cout << endl;
133 cout << "Press Enter to continue";
134 cin.ignore();
135 cin.get();
136 system(CLEAR_COMMAND);
137 }while(blnContinue);
138 return SUCCESSFUL_TERMINATION;
139 }

You might also like