Hello everyone

Am actually a newbie and learning C++ on my own. Have been trying to generate a code that prompts a student to enter his/details and after finishing. it couts his/her information. And prompts another student data to be entered. i have already some simple code here but it’s not giving my expectations.. Please help me on how to do it. +254718056203 is my contact add me in watsapp en help me. Hope am not a bother. Thanks

#include <iostream>
#include <string>



using namespace std;

int main()
{
  int admno;
  string studentname;
  string Addresscode;
  string telephonenumber;

  cout<<"please Enter admission number:\n";
  cin>>admno;
  cout<<"Please Enter student name:\n";
  getline(cin,studentname);
  cout<<"please Enter student telephone number:\n";
  getline(cin,telephonenumber);

  do {cout<<"admission Number: "<<admno<<"Address code: "<<Addresscode<<"Telephone number: "<<telephonenumber;}

while (admno!=35);
cin.get();


  }

Recommended Answers

All 5 Replies

Hi,
You can try this:

#include <iostream>
#include <string>
using namespace std;
int main()
{
int admno;
string studentname;
string Addresscode;
string telephonenumber;
do {
    cout<<"please Enter admission number(0 to terminate):\n";
    cin>>admno;
    if(admno == 0)
    {
        break;
    }
    cout<<"Please Enter student name:\n";
    getline(cin,studentname);
    cout<<"please Enter student telephone number:\n";
    getline(cin,telephonenumber);
    cout<<"admission Number: "<<admno<<"Address code: "<<Addresscode<<"Telephone number: "<<telephonenumber;
}
while (admno!=0);
cin.get();
}

Thanks, i tried ur code but its showing"please Enter student telephone number" at the same moment with "please Enter student telephone number" the loop is working but it doesnt prompt the user to enter registration number hence leaves an iput space

That comes from mixing input types. Mixing cin >> with getline() will cause this. One way to stop it is to put cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') after every call to cin >>. You will need to include the limits header for this to work.

why didn't you just use cin to take the strings instead of the getline..
anyway try this if it's the outcome u want. It's not so different from yours

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int admno;
  string studentname;
  string Addresscode;
  string telephonenumber;
  do
    { cout<<"\n\nplease Enter admission number:\n";
  cin>>admno;
  cout<<"Please enter Addresscode:\n";
  cin>>Addresscode;
  cout<<"Please Enter student name:\n";
  cin>>studentname;
  cout<<"please Enter student telephone number:\n";
  cin>>telephonenumber;

 cout<<"admission Number: "<<admno<<"\nAddress code: "<<Addresscode<<"\nTelephone number: "<<telephonenumber;
    }
    while(admno!=35&&admno>0);
  }

Thanks all u have actually solved my problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.