Question

In: Computer Science

Infile and getline - how can I change this code to get the whole line from...

Infile and getline - how can I change this code to get the whole line from a text file? Right now it only gets the first word each time it reads. NUMBER can be any integer. This code gets strings that are in multiple lines from a text file. Some words are separated by whitespace and that makes the input wrong. It only works if there is only one word.

infile >> arrayOne[dive] >> arrayTwo[dive];

while (infile && dive< NUMBER)
{
dive++;
infile >> arrayOne[dive] >> arrayTwo[dive];
}

infile >> arrayOne[dive] >> arrayTwo[dive];
}

Solutions

Expert Solution

In C++, if we need to read few sentences from a stream, the generally preferred way is to use getline() function. It can read till it encounters newline or sees a delimiter provided by user.

Here is a sample program in c++ that reads four sentences and displays them with ” : newline” at the end.

using namespace std;

int main()

{

    string str;

    int t = 4;

    while (t--)

    {

        // Read a line from standard input in str

        getline(cin, str);

        cout << str << " : newline" << endl;

    }

    return 0;

}

Getline In C++

While using C++, std::cin does not support accepting multiple lines in one go, to do this we have some in-built functions like getline. To accept a string or a line of input stream as input, we have an in-built function called getline(). This function is under the <string> header file.

It accepts all the strings until a newline character is encountered.

Inline function

It concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. ... The compiler can ignore the inline qualifier in case defined function is more than a line.


Related Solutions

C++ Text message decoder Use getline() to get a line of user input into a string:...
C++ Text message decoder Use getline() to get a line of user input into a string: Enter text: IDK if I'll go. It's my BFF's birthday. Search the string using find() for common abbreviations and replace() them with their long form. In addition, use a for loop to iterate through each character of the string and replace any occurences of '.' with '!': Enter text: IDK if I'll go. It's my BFF's birthday. I don't know if I'll go! It's...
Can I get a breakdown line by line of the time complexity, I know it is...
Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole. Input :- N , Array[N]                                function findSmallestPair()                                sort the Array                                int minimumDiff := INFINITY , A := -1 , B := -1;                                  for(int i : 1 to N-1)                                begin for                currentDiff = Array[i]-Array[i-1]                if(currentDiff < minimumDiff)                begin if   ...
This is a code for a bouncing ball on an 8X8 LED. How can i change...
This is a code for a bouncing ball on an 8X8 LED. How can i change the code to make it a ping pong game against AI by adding 1 potentionmeter to control it? #include <TimerOne.h>//this is a library that uses timer 1 of the arduino to trigger interrupts in certain time intervals //This defines a matrix defining a smiley face for the 8x8 LED matrix display #define BALL { \ {1, 0, 0, 0, 0, 0, 0, 0}, \...
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of...
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of user input into a string. Output the line. (3 pts) Ex: Enter text: IDK if I'll go. It's my BFF's birthday. You entered: IDK if I'll go. It's my BFF's birthday. (2) Search the string (using find()) for common abbreviations and print a list of each found abbreviation along with its decoded meaning. (3 pts) Ex: Enter text: IDK if I'll go. It's my...
Can I get whole answeres for this case? The case and questions in your side but...
Can I get whole answeres for this case? The case and questions in your side but I can see only five questions' answers but I need to see all solutions. The Sunrise Bakery Corporation was originally founded....... 6. What is the after-tax net income in each of the six years? 7. Calculate the change in working capital each year from the projected financial statements. 8. What is the terminal value of the project at the end of year 6? 9....
how can i get the code for this problem in visual basic? Create a Windows Forms...
how can i get the code for this problem in visual basic? Create a Windows Forms applications called ISP (Internet Service Provider) Project. You must enter the number of hours for the month and select your desire ISP package. An ISP has three different subscription packages for it's customers: Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2 per hours. Package B: For $13.95 per month 20 hours of access are provided. Additional...
Code is only reading the last line from the text files. How would I resolve? This...
Code is only reading the last line from the text files. How would I resolve? This is for the Name Search problem in Starting out with Python book. def main(): #open a file for reading infile = open('GirlNames.txt' , 'r') #Read the contents into a list Girls = infile.readlines() #open another file for reading infile = open('BoyNames.txt', 'r') #Read the contents into a lits Boys = infile.readlines() #Get a name to search for Name = input('Enter a name: ') #Determine...
Please explain clearly which line means what and how can I get the answer. (correct answer...
Please explain clearly which line means what and how can I get the answer. (correct answer 2 2) public class Checker { public static int count = 0; public int number = 0; public Checker(){ count++; number = count; } public int getCount() { return count; } public int getNumber() { return number; } } What is output from the code fragment below? Checker one = new Checker(); Checker two = new Checker(); System.out.println(one.getCount() + " " + two.getCount());
how can I change the Gauss-Seidel method to SOR method code in Matlab? The question has...
how can I change the Gauss-Seidel method to SOR method code in Matlab? The question has shows that In implementing SOR method in MATLAB, one should not calculate Tw and cw by formulas Tw = (D -wL)^(-1)[(1-w)D+wU)] and Cw = w(D-wL)^(-1)b , where w stands for omega and using MATLAB's built-in inv function, since this function requires O(n^3) flops and therefore the whole matter loses its point. I have tried for many times but I can't get the correct answers....
I was not sure how to utilize this line because I made code that works but...
I was not sure how to utilize this line because I made code that works but not with this line specifically. C++ Function 2: bool exists_trio_within_distance(int*,int,int); //Input:    //an integer array (param 1), its size (param 2), and    //a distance (param 3) //Output:    //True or false //Behavior:    //Returns true is there exists    //a sequence of 3 values in the array    //such that sum of the first two elements    //is equal to the third element...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT