DEV Community

dolphine
dolphine

Posted on

Errors during learning (c++)

we learn by doing

started my day by reading some articles
[http://www.applematters.com/article/10-things-every-programmer-should-know-for-their-first-job/]
and now I am solving some questions

Ps: the article has no relation to today's exercise

Another Error!!

I like how they make me think; esp the logical ones cause the code is clean but during compilation I see the computer talk back.
_ "Dont feed me this!"_
As you all know the computer is never wrong in its functions.
I aspire to be like the computer one day; how inhumane:)


Lesson:chaining inputs: when we have several inputs on one line we chain them

/*a program to calculate the sum of three numbers 
with getting input in one line separated by a comma.
Expected Output:
Input three numbers separated by comma: 5,10,15
The sum of three numbers: 30*/

#include <iostream>

int main() {

    int a;
    int b;
    int c;
    int sum;

    std::cout << "Input three numbers separated by comma: " << std::endl;
    std::cin >> a >> b >> c;

    sum = a + b + c;

    std::cout << "The sum of three numbers:" << sum << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Ps: The main quuestion has been partiallly solved but when you use comas you get wonders
To use space the input


previous code

//line 11
std::cin >> a,b,c;
Enter fullscreen mode Exit fullscreen mode

Error:

``` q9.cpp:17:19: warning: left operand of comma operator has no effect [-Wunused-value]
std::cin >> a,b,c;
^
q9.cpp:17:21: warning: expression result unused [-Wunused-value]
std::cin >> a,b,c;
^



**type of Error**: Syntax

Notes

**why it's failing:**
In C++, the comma operator evaluates expressions from left to right but only returns the result of the last one. 
* In std::cin >> a, b, c;, the compiler first executes std::cin >> a (which works).
* It then sees , b and , c. Because these are not attached to std::cin using the correct operator, the compiler simply looks at the values of b and c and then discards them.
* The warnings tell you that b and c are "unused" because the code isn't actually storing any input into them

How to fix it:
To read multiple variables in one line, you must use the extraction operator (>>) for every variable. 

Correct Syntax:


```cpp
std::cin >> a >> b >> c ;
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • Chaining Operators: Each >> tells the program to take the next piece of data from the input stream and store it in the following variable.
  • Separation: When running the program, the user can separate these inputs with spaces, tabs, or newlines.
  • Precedence: The comma has the lowest priority of all operators in C++. Using it here causes the compiler to treat the input request as three separate, unrelated expressions rather than a single linked input command.

reference W3 schools, Gemini


"Another one..." 'anada banger' 'another story -ohh'
One last fix
the question was not hard; I wondered why I was encountering it among the last sections. Now I know.

Error: Wrong output with commas but its ok without
On a normal day, this could've been ignored and we won't have been here lads.

To allow commas in your input, you must explicitly "catch" each comma using a character variable. In C++, std::cin automatically skips whitespace (like spaces or tabs), but it stops when it encounters a comma while trying to read an integer.

Why the code failed
When you type 5,10,15, std::cin >> a reads the 5. It then sees the comma, realizes it's not a number, and stops. The comma remains in the input buffer, blocking b and c from being read properly.

The Solution:
quite simple but easy to miss
Use a char variable (like ch) to "consume" the comma between each number.

How this works:
std::cin >> a: Reads the first integer (5).

ch: Reads the next character (the comma ,) and stores it in ch.
b: Now that the comma is gone, it reads the next integer (10).
ch: Reads the second comma.
c: Reads the final integer (15).

final_code

/*a program to calculate the sum of three numbers 
with getting input in one line separated by a comma.
Expected Output:
Input three numbers separated by comma: 5,10,15
The sum of three numbers: 30*/

#include <iostream>

int main() {

    int a;
    int b;
    int c;
    int sum;
    char ch;

    std::cout << "Input three numbers separated by comma: " << std::endl;
    std::cin >> a >>ch >> b >>ch>> c;

    sum = a + b + c;

    std::cout << "The sum of three numbers:" << sum << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Ps: You do not assign any value(,) to ch. I tried and got some error.


No errors =clean code = satistfaction
&
erros = new knowldge = learning

.mwaitsi

Things do come handy in the long run
alternative code for question 5 came in handy for question 10

#include <iomanip>

std::cout << std::fixed << std::setprecision(6);

// for specific decimal point precision
// 6 decimal points

Enter fullscreen mode Exit fullscreen mode

Thought you should know
if the user puts spaces after commas, like 5, 10, 15, which is a common real-world input in:

#include <iostream>

int main() {

    int a;
    int b;
    int c;
    int sum;
    char ch;

    std::cout << "Input three numbers separated by comma: " << std::endl;
    std::cin >> a >>ch >> b >>ch>> c;

    sum = a + b + c;

    std::cout << "The sum of three numbers:" << sum << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

here it won't work

what happens is that when a space is added the 2nd number is ignored
Solution
addition of spaces

#include <iostream>

int main() {

    int a;
    int b;
    int c;
    int sum;
    char ch;

    std::cout << "Input three numbers separated by comma: " << std::endl;
    std::cin >> a >> ch >> b >> ch >> c;

    sum = a + b + c;

    std::cout << "The sum of three numbers:" << sum << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

pretty simple and easily neglible
but it works!
and it helped with this question

/*Write a C program to find the third angle of a triangle if two angles are given.
Expected Output :
Input two angles of triangle separated by comma : 50,70
Third angle of the triangle : 60*/

#include <iostream>
int main() {

    int x ;
    int y ;
    int z;
    char c;

    std::cout << "Input two angles of triangle separated by comma : " << std::endl;
    std::cin>> x >> c >> y;

    z = 180 - (x + y);
    std::cout << "Third angle of the triangle : " << z << std::endl;


    return 0;
}
Enter fullscreen mode Exit fullscreen mode

In life everything is interconnected and so is here

Top comments (0)