Question
Code in C++
Function Prototypes
For the second part of the lab activity, you will be practicing writing function prototypes. Continue working on the same pro
int a; 1 #include <iostream> 2 #include <string> 3 #include <limits> 4 5 using namespace std; 6 7e int getNumber() 8 { 9 10 c
The following is an example of how your program should run if just repeating once: Enter 1 to repeat one message or enter 2 t
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream> #include <string> #include <limits using namespace std; // these are the prototypes before the main funci27 20 //main function start of execution 21 int main() 22 { 23 // declare the variabel choice to know user choice 24 int choi// check if the choice is 2 then do the following if (choice == 2 ) { // make a call to the getNumber to get number times to67 64 // definition of the funcitons 65 66 //this method return the value entered by the user int getNumber ( ) 689 { 69 70 i82 // This function reads the string and return the string 84 string getMessage(){ 85 86 // the below line ignore the new lin102 103 //this function repeat one string only 104 void repeat (int a, string b){ 105 106 // using for Loop to print the striC:\Desktop\dev\Prototypes.exe Enter 1 to repeat one message or enter 2 to repeat two messages: 1 Enter a number: 5 Enter a meC:\Desktop\dev\Prototypes.exe Enter 1 to repeat one message or enter 2 to repeat two messages: 2 Enter a number: 3 Enter a me

Code:

#include <iostream>
#include <string>
#include <limits>
using namespace std;

// these are the prototypes before the main funciton
// the function getNumber takes a number from the user
int getNumber ();

// the function getMessage takes the message from the user
string getMessage();

//the funciton repeat the only one string
void repeat (int , string );

//the function repeat the two strigs
void repeatTwo(int , string , string );


//main function start of execution
int main()
{
//   declare the variabel choice to know user choice
   int choice;
  
//   ask the user to enter 1 or 2
   cout<<"Enter 1 to repeat one message or enter 2 to repeat two messages: ";
   cin>>choice;
  
//   check if the user entered 1 if so then do the following
   if(choice == 1){
  
//   make a call to the getNumber to get number times to repeat string
   int a = getNumber( );

//   make a call to getMessage to get the message form the user
   string b = getMessage( );
  
//   Then call the method repeat to print the string a times
   repeat (a, b);
  
   }
  
//   check if the choice is 2 then do the following
   if(choice == 2 ){
      
//       make a call to the getNumber to get number times to repeat string
       int a = getNumber();
      
//       make a call to getMessage to get the message from the user string 1
       string b = getMessage();
      
//       make a call to getMessage to get the message from the user string 2
       string c = getMessage();
      
//       now make a call to the repeatTwo and pass the a and two strings to print
       cout<<"\n";
       repeatTwo(a,b,c);
   }

return 0;
}

// definition of the funcitons

//this method return the value entered by the user
int getNumber ( )
{
  
   int a;
  
//   ask the user to enter a number
   cout <<"Enter a number: ";
  
//   store the number in a
   cin >> a;
  
//   return the number
   return a;
}


// This function reads the string and return the string
string getMessage(){
  
//   the below line ignore the new line after one input is taken
   cin.ignore(numeric_limits < streamsize>::max(),'\n' ) ;


   string b;

//    ask the user to enter the string
   cout << "Enter a message: ";

//   read the string to b
   getline(cin,b);

//   return the string
   return b;

}

//this function repeat one string only
void repeat (int a, string b){
  
//   using for loop to print the string
   for (int i=0; i<a; i++){

//       print the string
       cout <<b<<" \n";
}

}

//this function takes two strings and print them
void repeatTwo(int a,string b,string c){
  
//   use while loop until a becomes zero iterate it.
   while(a--){
      
//       print the strings
       cout<<b<<"\n";
       cout<<c<<"\n";
   }
}


Note : If you have any queries regarding the code please make a comment.

Add a comment
Know the answer?
Add Answer to:
Code in C++ Function Prototypes For the second part of the lab activity, you will be...
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
  • #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...

    #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes here: int main() {     cout << "Welcome to Mad Lib.\n\n";     cout << "Answer the following questions to help create a new story.\n";     string name = askText("Please enter a name: ");          string noun = askText("Please enter a plural noun: ");          int number = askNumber("Please enter a number: ");          string bodyPart = askText("Please enter a body part: ");          string...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • c++ please include comments 4. Rewrite the code below including the definitions of the function prototypes...

    c++ please include comments 4. Rewrite the code below including the definitions of the function prototypes #include <iostream> #include <iomanip> #include <string> #include <stream> using namespace stod bool disjunction(bool.bool) bool negation(bool): bool implication(bool,bool bool equi bool,bool char btoc(bool value) return (value)?(T") ('F); string truthTable); int main( cout << truth Ta return 0; The functions conjunction), disjunction), negation), implication), and equivalence) should return the truth value of the conjunction, disjunction, negation, implication and equivalence connectives respectively. The functions truthTable) return a...

  • Give the output for the following code assume int globalvar 10: above the function prototypes void...

    Give the output for the following code assume int globalvar 10: above the function prototypes void foo(int&); void bar(int); int main() { int globalVar = 5; foo(globalVar); cout << globalVar << endl; bar(globalVar); cout << globalVar << endl; return 0; } void foo(int& x) { x = globalVar + x; cout << globalVar << endl; return; void bar(int x) { globalVar + x; cout << globalVar << endl; X = return;

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • For this lab, you will need to write the following functions in table.cpp, and add function...

    For this lab, you will need to write the following functions in table.cpp, and add function prototypes for them to table.h and invoke the functions in main.cpp. * int countNodes(node * root) Recursively count the number of the nodes in the tree. * int sumLeaves(node * root) Recursively sum all of the leaf nodes in the tree. You must use the functions with these exact function prototypes. ****************************************************************************** main.cpp #include "table.h" #include <iostream> using namespace std; int main() { node...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • C++ i have started the program i just need help finishing it. This chapter is on...

    C++ i have started the program i just need help finishing it. This chapter is on functions. If you write this assignment without creating functions (main itself does not count) then the maximum score you can receive is 10% of the grade even if the output is perfectly correct. Write a modular program with the following Functions: Main, PrintHeading, PrintTuitionTable The Main Function should ask the user for a Tuition like assignment 6. Then it should call the PrintHeading function,...

  • in c++ Finish the methods in the following code. DO NOT CHANGE THE MAIN METHOD. The...

    in c++ Finish the methods in the following code. DO NOT CHANGE THE MAIN METHOD. The instructions for each method are in a comment in the template. DO NOT CHANGE THE MAIN METHOD!!!! #include <iostream> #include <string> using namespace std; // prototypes int main() { int answer = 0; int value1 = 0; int value2 = 0; double average = 0; string string1 = ""; int numberVowels = 0; int numberNonVowels = 0; cout << "Enter string1: "; getline(cin, string1);...

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