Question

Why must getline() be used instead of cin to get string input? Give an example of...

  1. Why must getline() be used instead of cin to get string input?
    1. Give an example of when you would need to use cin.ignore() in your code
    2. Strings in C++ are mutable, this is different than in Java where strings were immutable. What do we mean by mutable versus immutable?
    3. Imagine your code had the following string variable in it string message = "HH231JJ"; Write a for loop that uses the [ ] operator to change each 'H' character in the message string to a '#' character.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer a.

Code Explanation

#include <iostream>
#include <string>

using namespace std;

int main(){
string str, str1;

/*cin is not able to read string with more than one word
thats why we use getline()*/

cout<<"Enter string using getline: ";
getline(cin, str1);
cout<<"Enter string using cin: ";
cin>>str;

cout<<"cin string: "<<str<<endl;
cout<<"getline string: "<<str1;
}

Output

Answer b

When you use cin for input it takes space " ", "\t", "\n" as a delimiter character. It doesn't display it but stores it in the buffer, for example,

int a;

string str

cin>>a

//use cin.ignore() here to make it work properly

getline(cin, str);

After you press in cin it will skip the getline() because the getline() will read "\n". So to flush the buffer, we use cin.ignore().

Answer c

In Java, strings are immutable which means that once the value stored in the string variable it cannot be changed. If you change the value it will allocate a new space in the string pool memory for that new value. But in C++ the strings are mutable which means that the stored value can be changed without allocating new memory in the string pool. We can achieve string mutability in Java as well using StringBuilder and StringBuffer.

Answer d

Code Explanation

#include <iostream>
#include <string>

using namespace std;

int main(){
string str="HH231JJ";
for(int i=0; i<str.length(); i++){
//using [] operator to find H
if(str[i]=='H'){
str[i]='#';//replacing H with #
}
}
cout<<str;
}

Output

Add a comment
Know the answer?
Add Answer to:
Why must getline() be used instead of cin to get string input? Give an example of...
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
  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • A bounded string list has an upper limit on its size, which is set when the...

    A bounded string list has an upper limit on its size, which is set when the list is created. The size limit specifies the maximum number of strings that can be stored in a bounded string list. String lists are mutable and allow random accesses to their elements using indexes. The list operations include BoundedStringList(int sizeLimit); void insert(String newString, int index); void delete(int index); String get(int index); The constructor creates an empty list with a maximum size of sizeLimit. The...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

  • Explain why the following code will give different output in Java String strl="DSU"; String str2="DSU"; Apply...

    Explain why the following code will give different output in Java String strl="DSU"; String str2="DSU"; Apply the binary division to the following input. Show all your steps in the algorithm Input dividend n=68 and divisor d=12. Determine quotient q. Show all your steps

  • Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an...

    Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be...

  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • TASK Your task is to build a palindrome from an input string. A palindrome is a...

    TASK Your task is to build a palindrome from an input string. A palindrome is a word that reads the same backward or forward. Your code will take the first 5 characters of the user input, and create a 9- character palindrome from it. Words shorter than 5 characters will result in a runtime error when you run your code. This is acceptable for this exercise – we will cover input validation in a later class. Some examples of input...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

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