Question

Use Repetitive sentences to explain this code by ( For or While or Do While)using namespace std; #include<iostream> #include<math.h> main() float OH_Vinegar-pow(10, -12); float OH_Soap=pow(10,-2); //OH


amend the code by use with any of the loop :
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

=============================== Welcome to pH calculator To calculate the concentration of H+ ions choose one of the followin

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!!

Add a comment
Know the answer?
Add Answer to:
Use Repetitive sentences to explain this code by ( For or While or Do While) amend...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code is in C++: Im wondering how i can make the program continue to ask the...

    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;...

    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...

    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"; }

  • What did I do wrong with this C++ code? Assume that we don't need to ask...

    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...

    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...

    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?...

    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,...

    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;...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • Plz give me correct code and screen shots for this question by using SASM !!!!!!!! Implement...

    Plz give me correct code and screen shots for this question by using SASM !!!!!!!! Implement the following working C++ program in assembler. Submit assembler code and screen shot. #include <iostream> #include <iomanip> using namespace std; // This menu-driven Health Club membership program carries out the // appropriate actions based on the menu choice entered. A do-while loop // allows the program to repeat until the user selects menu choice 4. int main() { // Constants for membership rates const...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT