Use Repetitive sentences to explain this code by ( For or While
or Do While)
By "amend", I'm guessing you need to repeatedly display the menu till user exits. You can use a do while loop in that case with the help of a switch statement. Something like -
#include <iostream> #include <math.h> using namespace std; int main() { float OH_Vinegar = pow(10, -12); float OH_Soap = pow(10, -12); float OH_Water = pow(10, -12); float OH_Blood = pow(10, -12); float OH_Milk = pow(10, -12); float H_Vinegar, H_Soap, H_Water, H_Blood, H_Milk, pH; int Choice; do{ cout<<"\n================================================\n"; cout<<"Welcome to pH calculator\n"; cout<<"================================================\n"; cout<<"To calculate the concentration of H+ ions choose one of the following substances:\n 1-Vinegar \n 2-Soap \n 3-Water \n 4-Blood \n 5-Milk \n 6-Exit\n"; cin>>Choice; //Do your work as per the chosen alternative //do your required work here: //I've used switch in this case, which is probably what you want to do. switch(Choice){ case 1: cout<<"\nYou chose 1-Vinegar"; break; case 2: cout<<"You chose 2-Soap"; break; case 3: cout<<"You chose 3-Water"; break; case 4: cout<<"You chose 4-Blood"; break; case 5: cout<<"You chose 5-Milk"; break; case 6: exit(0); } }while(Choice != 6); return 0; }
And take a look at the output:

As soon as we enter 6, the program stops. So you always need to include such an option to exit the executin flow.
The sole purpose I used a Do-While loop, rather than for/while loop is that it will execute at least once, no matter what the user wants to do.
Hope it helps. Best of Luck!!
Use Repetitive sentences to explain this code by ( For or While or Do While) amend...
Code is in C++: Im wondering how i can make the program continue to ask the user to enter Y/N with the if statment. Is it possible with an If statment? #include <iostream> using namespace std; int main() { float usDollars,cYuan; float *Dollars; char choice; Dollars = &usDollars; while(usDollars >= 0){ cout <<"Enter the amount in U.S Dollars: "; cin >> usDollars; cout << usDollars<< " U.S Dollar in Chinese Yuan is :"<<*Dollars*7.09<<endl; if (choice == 'y' || choice ==...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
I need to add a for or a while loop to the following code. please help needs to be in c++. #include <iostream> #include <string.h> using namespace std; int main() { char input[100]; cout << "Please enter a character string: "; cin >> input; char *head = &input[0], *tail = &input[strlen(input) - 1]; char temp = *head; *head = *tail; *tail = temp; tail--; head++; cout << "CString = " << input << "\n"; }
Hi so our professor is making us re engineer this code but so confuse about it for c++ and yes I'll rate you up thank you^^ re-engineer the code to handle the case of (milk <= 0) with exception handling. #include using namespace std; int main() { int donuts, milk; double dpg; cout << "Enter number of donuts:\n"; cin >> donuts; cout << "Enter number of glasses of milk:\n"; cin >> milk; if (milk <= 0) { cout << donuts...
What did I do wrong with this C++ code? Assume that we don't need to ask users for the number of students. #include <iostream> #include <iomanip> using namespace std; int main() { float *grades; // a pointer used to point to an array int size; int count = 0; // track the number of grade/student float grade; // the grade of a student float average, total; // average grade and total grades //**************start your code here************************ cout<<"How many student grades...
2. What is the value of x alter the following code is executed # include<iostream> using namespace std; int main int t-0, c- 0,x-3; while (c < 4) t=t+x; cout << x return 0; a) 3 b) 21 c) 24 d) 48 3. What is the output of the following code? # include<iostream> using namespace std; int main0 int a- 3; for (int i 5; i >0; i) a a+i cout << a << “ “ return 0; d) 8...
What is the software bug, and how do you fix the issue for the code fragment below? #include <iostream> #include <limits> using namespace std; int main() { int x; //find the maximum integer value and avoid an overflow int imax = std::numeric_limits ::max(); cout <<"Enter a value for x: "; cin >> x; if (x > imax) cout << "overflow\n"; else cout << x; return 0; }
Why this C++ code output is 1 1 num is 3 Can u explain to me? Thank u code: #include using namespace std; void xFunction(int num) { do { if (num % 2!= 0) cout << num << " "; num--; } while (num >=1); cout << endl; } int main() { int num = 1; while (num <= 2) { xFunction(num); ++num; } cout << "num is " << num; return 0; }
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
Please help! Studying for my c++ test! What does the following code produce? include <iostream> using namespace std; void my_function(int n); void main() { my_function(546); } void my_function(int n) { if (n < 10) cout << n << endl; else { my_function(n/10); cout << (n%10) << endl; } } What is the output of the following code? int j=32, k=5, r; r = j ^ k; cout << r...