-2

this is my code and result I just create a structure first and then set up a for loop to use that elements and take input from user and display at last of the code part. please help me

#include <iostream>
using namespace std;
struct person
{
    char name[30]; 
    int age;
    int phone_no;
    
}p[5];

int main()
{
    for (int i=0; i<5; ++i)
{    
    cout<<"Enter you details \n\n\n";
    cout<<"     Enter your name : ";
    cin.get(p[i].name,30);
    cout<<"\n       Enter your age : ";
    cin>>p[i].age;
    cout<<"\n       Enter your phone no : ";
    cin>>p[i].phone_no;
}
for (int i=0; i<5; ++i)
{
    cout<<"\n\n\n Person's name : "<<p[i].name<<"\n Age : "<<p[i].age<<"\n Phone no : "<<p[i].phone_no;
}

return 0;

} 

result

Enter you details


                Enter your name : Ram

                Enter your age : 12

                Enter your phone no : 9898874645
Enter you details


                Enter your name :
                Enter your age :
                Enter your phone no : Enter you details


                Enter your name :
                Enter your age :
                Enter your phone no : Enter you details


                Enter your name :
                Enter your age :
                Enter your phone no : Enter you details


                Enter your name :
                Enter your age :
                Enter your phone no :

 Person's name : Ram
 Age : 12
 Phone no : 2147483647


 Person's name :
 Age : 0
 Phone no : 0


 Person's name :
 Age : 0
 Phone no : 0


 Person's name :
 Age : 0
 Phone no : 0


 Person's name :
 Age : 0
 Phone no : 0
Process exited after 12.16 seconds with return value 0
Press any key to continue . . . 

I also want to u add some useful advice if you have regarding my code skill

MikeCAT
  • 61,086
  • 10
  • 41
  • 58
jas
  • 3
  • 2
  • 1. The phone number was too large to store in `int`. It should be stored as a string. – MikeCAT Jan 12 '21 at 19:14
  • 2. There seems a problem like this: [c++ - Why does std::getline() skip input after a formatted extraction? - Stack Overflow](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – MikeCAT Jan 12 '21 at 19:14
  • @MikeCAT _"2. ..."_ Nope the problem (solution) is rather described [here](https://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it). The one you propose only occurs in conjunction with `std::getline()`. – πάντα ῥεῖ Jan 12 '21 at 19:32

2 Answers2

1

The problems are:

  • The phone number is too large to fit into int in this environment. It should be stored as strings.
  • >> operator for cin leaves newline characters at the stream, so the next cin.get() will read that and stop there. You can read until newline character and drop what are read via cin.ignore(). Note that cin.get() also leaves newline character, but >> operator kindly ignores that.

Try this:

#include <iostream>
#include <limits>
using namespace std;
struct person
{
    char name[30]; 
    int age;
    char phone_no[30];
    
}p[5];

int main()
{
    for (int i=0; i<5; ++i)
    {
        cout<<"Enter you details \n\n\n";
        cout<<"     Enter your name : ";
        cin.get(p[i].name,30);
        cout<<"\n       Enter your age : ";
        cin>>p[i].age;
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        cout<<"\n       Enter your phone no : ";
        cin.get(p[i].phone_no,30);
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    for (int i=0; i<5; ++i)
    {
        cout<<"\n\n\n Person's name : "<<p[i].name<<"\n Age : "<<p[i].age<<"\n Phone no : "<<p[i].phone_no;
    }

    return 0;

}
MikeCAT
  • 61,086
  • 10
  • 41
  • 58
  • thanks for your answer and time i am a beginner finding solution for that problem and redirected to this website i don't find my answer so post my problem, thanks again teaching me – jas Jan 14 '21 at 15:33
-2

Ah, i really don't know why but c++ has trouble with cin.get(); It works fine with cin:

#include "iostream"
using namespace std;
struct person
{
    char name[30]; 
    int age;
    int phone_no;
    
}p[5];

int main()
{
    for (int i=0; i<5; ++i)
{    
    cout<<"Enter you details \n\n\n";
    cout<<"     Enter your name : ";
    cin>>p[i].name;
    cout<<"\n       Enter your age : ";
    cin>>p[i].age;
    cout<<"\n       Enter your phone no : ";
    cin>>p[i].phone_no;
}
for (int i=0; i<5; ++i)
{
    cout<<"\n\n\n Person's name : "<<p[i].name<<"\n Age : "<<p[i].age<<"\n Phone no : "<<p[i].phone_no;
}

return 0;

} 
scohe001
  • 13,879
  • 2
  • 28
  • 47