Write a function called getsentence that gets the initial in

Write a function called get_sentence() that gets the initial input string/sentence. Create a string object to read the string from the user, #include . Since, a user can enter a single word or a string with spaces, you will need to use the getline() function to read the string/sentence from the user, i.e. getline(cin, s). Use this function to get the string/sentence from the user and print the contents of the string after making the call to this function. For example: int main() { string sentence; sentence = get_sentence(); cout << sentence << endl; return 0; } The prototype for this function is as follows. You need to determine pre-conditions and post-conditions for this function. The preconditions are anything you can say holds true about the function parameters before the function is called, and the postconditions are anything you can say holds true about the parameters, after the function executes. string get_sentence(); Now, change the get_sentence() function so that it is a void function, which means you cannot assign the value returned from the function call to the sentence variable in main! Since the function is now a void function, it has to modify the string parameter directly, instead of returning information. You need to add an & to the string parameter. void get_sentence(string &s); The & in the parameter list tells us that this is a reference to string, and the string can be changed inside the function. Write the function without the & in the parameter listing, what do you notice? Write the function with the & in the parameter listing, what do you notice? What are the preconditions and postconditions for void get_sentence(string &s)?

Solution

1)

#include<iostream> //header files

#include <string>

using namespace std;

string get_sentence(){ // called function with no arguments

string sent; // declaring req variables

getline(cin,sent); // getting sentence from user

return sent; // returning that sentence

}

int main(){

string sentence; // declaring req variables

sentence=get_sentence(); // calling a function which takes input

cout<<sentence; // printing it

}

The pre condition are that we have to call the function in a syntactically wriiten main.

The post condition is to have a variable where a value is to be catched when it is returned from a get_sentence() method.

2)

#include<iostream> //header files

#include <string>

using namespace std;

void get_sentence(string &s){ // called function with an arguments address is passded

getline(cin,s); // getting sentence from user

cout<<s; // printing sentence

}

int main(){

string sentence; // declaring req variables

get_sentence(sentence); // calling a function which takes input paasing the addres of the decalred variable

}

The pre condition are that we have to decalare a string datatype variable and it is to be passes and the formal arguments should be the address.

Their is no post condition as their is no task to do after the execution of the task. If we talk in particular, the post condition will the sentence should be displayed in command promt.

Write a function called get_sentence() that gets the initial input string/sentence. Create a string object to read the string from the user, #include . Since, a
Write a function called get_sentence() that gets the initial input string/sentence. Create a string object to read the string from the user, #include . Since, a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site