-3

Running this code block skips the second cin call (cin>>color;) and passes it onto the next line. Can someone point out my mistake?

#include <iostream>
using namespace std;
int main()
{
    char cod;
    string colour,material;
    float abs,ord,r;

    cout<<" CODUL : " ; cin>>cod;
    cout<<"\nCOLOUR : "; cin>>colour;
    cout<<"\nMATERIAL : "; cin>>material;
    cout<<"\nABSISA :  "; cin>>abs;
    cout<<"\nORDONATA : " ; cin>>ord;
    cout<<"\nRAZA : " ; cin>>r;
}
Spevacus
  • 584
  • 2
  • 13
  • 23
  • 1
    What do you input for `CODUL :`? – NathanOliver Jun 13 '19 at 20:24
  • @NathanOliver 00141 – Alessandro Jun 13 '19 at 20:27
  • 2
    Unrelated, you're missing `#include ` in this code. It's *required* to properly use `std::string` (which you're using via `using namespace std;`, [which is a terrible idea regardless](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?r=SearchResults&s=1|1079.3379)). Related, running this in a *debugger* would immediately show you what is actually being consumed (or not) from `cin>>colour`. Given your input, it will become obvious. – WhozCraig Jun 13 '19 at 20:28
  • 5
    What is `cod` a `char` if you want a number? – NathanOliver Jun 13 '19 at 20:28
  • @NathanOliver if i use int , 0 will not be displayed – Alessandro Jun 13 '19 at 20:35
  • 3
    Use a string then if you want to preserve leading zeros – NathanOliver Jun 13 '19 at 20:35
  • 1
    Your code reads one character (`cod`) and then it reads a string (`colour`), so if you enter "00141", it will get '0' (the digit zero, not the number zero) for the character and "0141" for the string. – David Schwartz Jun 13 '19 at 21:35

2 Answers2

1

You entered more than a single character. Read a whole line, and parse the line.

Example, running your program, I enter "xyzzy\n" for "CODUL":

./stuff
CODUL : xyzzy
cod:x

COLOUR : colour:yzzy

MATERIAL :

For this revision to your program:

int
main()
{
char cod;
string colour,material;
float abs,ord,r;

cout<<" CODUL : " ; cin>>cod; cout<<"cod:"<<cod<<endl;
cout<<"\nCOLOUR : "; cin>>colour; cout<<"colour:"<<colour<<endl;
cout<<"\nMATERIAL : "; cin>>material; cout<<"material:"<<material<<endl;
cout<<"\nABSISA :  "; cin>>abs; cout<<"abs:"<<abs<<endl;
cout<<"\nORDONATA : " ; cin>>ord; cout<<"ord:"<<ord<<endl;
cout<<"\nRAZA : " ; cin>>r; cout<<"ord:"<<ord<<endl;
}
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
0

You forgot #include<string>. You cannot use strings without it. Also do you want 'cod' to only take one character? Because a character can only hold one number/letter/special character. If you want it to hold more than one character than you need to declare it as string. Or you could declare more than one variable for each character you type on that line. Anyways I updated your code, and it worked fine for me.

#include <iostream>
#include<string>
using namespace std;
int main()
{
    char cod;   //If you want more than one character declare it as string or 
               // create multiple char variables to store the multiple
              //  variables in
    string colour,material;
    float abs,ord,r;

    cout<<" CODUL : "; cin>>cod;
    cout<<"\nCOLOUR : "; cin>>colour;
    cout<<"\nMATERIAL : "; cin>>material;
    cout<<"\nABSISA :  "; cin>>abs;
    cout<<"\nORDONATA : "; cin>>ord;
    cout<<"\nRAZA : "; cin>>r;

    system("pause");
    return 0;
}

EDIT #1

Just an updated piece of code showing how to enter multiple different char and they all get stored in multiple variables. The below code was tested and works correctly. Assuming this is what you are looking for.

#include <iostream>
#include<string>
using namespace std;
int main()
{
    char cod, cod_1, cod_2, cod_3;   //If you want more than one character inputted for cod then                         //declare it as string or 
              // create multiple char variables to store the multiple
             //  variables in
    string colour,material;
    float abs,ord,r;

    cout<<" CODUL : "; cin>> cod >> cod_1 >> cod_2 >> cod_3;
    cout<<"\nCOLOUR : "; cin>>colour;
    cout<<"\nMATERIAL : "; cin>>material;
    cout<<"\nABSISA :  "; cin>>abs;
    cout<<"\nORDONATA : "; cin>>ord;
    cout<<"\nRAZA : "; cin>>r;

    system("pause");
    return 0;
}
Tim
  • 191
  • 2
  • 28