Sort 3434 Discussions, By:
Please Login in order to post a comment
People starting to learn C++ might suffer if they aren't familiar with cin >> and getline() and what happens if you use both of them together. Using cin leaves an end of line character which is then read by your getline(); It is best not to mix different types of input.
The solution one could use to overcome this problem is to use a different input method. On the other hand, it is always nice to get familiar with problems one might come across while coding, so take a look. It is possible to overcome this problem while keeping all of your code and just adding a single line.
Here is an excerpt from the input code:
cin >> doubleNumber;
cin.ignore(); //ignores an end of line character
This will read you string correctly, it will also read the whole line. It will just ignore a character and continue from that.
I could be wrong, I'm still a complete novice myself but this is the way I understand it. Cheers.
For C programmers::
(this is not the comment.this is the solution of facing prob in reading input).
Now I think all c programmers facing the problem(in this question) that either they are unable to get proper input or how to take input till end line(new line).
For solution I need to ask u something.
Do u know why we use spaces between scanf()?
we use spaces because spaces inside scanf() make scanf() to take input skipping the spaces of given input by user.
Given question will give us a new line after the double input.
So if we use scanf("%[^\n]"), we will not get the line we wanted because it will stop taking new line as it got earlier.So we need to skip the new line before taking input of string till new line.
As I said earlier we can use \n inside scanf() to skip that new line
so, our code might look like this:
scanf(" %d %lf\n%[^\n]", &[int variable], &[double variable],[your string]);
now the scanf() will work and u will be happy getting 30 marks.
Note:this can be done by assingnment supperssion or many other ways.Oh!yes, the gets() is banned coz it may make Buffer Overflow(security threat).You may use fgets().
String nextLine(): Returns the next line of text, or, if you are in the middle of a line, returns the remainder of the line. Caution: If you are in the middle of a line, nextLine does not return the next line, but instead the remainder of the current line.
I guess this is why we have to add a
before declaring the second String variable.
I would like to know if there is a better solution to this problem.
This is how I solved this challenge:
int i2 = scan.nextInt();
double d2 = scan.nextDouble();
String s2 = scan.nextLine();
System.out.println( i + i2);
System.out.println(d + d2);
System.out.println(s + s2);
P.s.: For those of you (like me) who try to learn Java and solve the challenges via the video tutorial given here, it doesn't closely follow the challenges. So be sure to check the instructions given instead of trying to implement what you have learnt from the video.
P.p.s: I am new to coding, and I find the instructions and the written tutorial quite confusing and hard to understand. They sound like they have been written for those who are already familiar with coding. In the Day 1 challenge, it says:
/* Declare second integer, double, and String variables. */
/* Read and save an integer, double, and String to your variables.*/
Although I completed this challenge successfully- at least it says so-, I don't know why "declare" and "read and save" are written separately while you can do those things on the same line . I guess, "declaring" should be assigning a value to a variable via "=". Then "read and save" should be the "scan.nextLine()" part. The instructions would be more useful if these stuff were explained a little bit better. As in " 'scan.next.Line' method is used to 'read and save' the variables and you can 'declare, read and save' a variable using only one line like ....".
For Python programmers:
The key to solve this problem is undesrtanding how to read "stdin". Take a look at "Standard File Objects" section on this guide:
// Declare second integer, double, and String variables.
// Read and save an integer, double, and String to your variables.
i1 = parseInt(readLine());
d1 = parseInt(readLine());
s1 = readLine();
// Print the sum of both integer variables on a new line.
console.log(i + i1);
// Print the sum of the double variables on a new line.
console.log((d + d1).toFixed(1));
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
console.log(s + s1);