Jump to content
  • Advertisement
Sign in to follow this  
OpperationQuak

[Resolved] Basic I/O problem in C++

This topic is 3551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Resolved Hi there i am writing a program to write user inputted data to a database running in a loop but everytime it runs the loop more than once you dont get a chance to enter the first set of information and it goes straight to the second set of information eventho they are both using the same function to get the input heres what i get when i run the program: image my code is in a later post any help would be greatly appreciated [Edited by - OpperationQuak on October 29, 2008 7:22:38 AM]

Share this post


Link to post
Share on other sites
Advertisement
You're making a basic problem very complicated. My advice is to just use cin directly instead of getInput, until you find yourself with duplicated code that needs to be factored out.

Share this post


Link to post
Share on other sites
i understand that it does look like im making a simple problem complicated but the program as a whole isnt just going to write a database

also if i just use cin i will never find the problem and therefore wont learn what i have done wrong

Share this post


Link to post
Share on other sites
Quote:
Original post by OpperationQuak
i understand that it does look like im making a simple problem complicated but the program as a whole isnt just going to write a database
The only thing that your getInput fnction accomplishes is to needlessly complicate a very simple problem. Here is a simple refactoring of your code, with no need for get input, no need for a separate parsing structure, and simpler to boot:
	do
{
input userInput;
cout << "Enter Name: "
getline(cin, custDetails.name);
cout << "Enter Address: "
getline(cin, custDetails.address);
cout << "Enter Phone Number: "
getline(cin, custDetails.phoneNum);
cout << "Enter D.O.B: "
cin >> custDetails.dob;

cin >> std::boolalpha >> cont;
}
while(cont == true);

Share this post


Link to post
Share on other sites
i get what your saying and i know i could do that but i want to find out whats causeing the problem with the function

if i make it an infinite loop and stop getting the date and continue prompt it works fine so it something to do with one of the 3 lines of code im omiting

anyone have any ideas without telling me to stop using getInput?

Share this post


Link to post
Share on other sites


#define NAME_MAX 256
#define ADDR_MAX 256

struct Customer
{
char Name[NAME_MAX];
char Address[ADDR_MAX];
int PhoneNo;
int DOB;
};


void prompt(const char* msg, bool newLine = true)
{
if(newLine)
{
cout << msg;
}
else
{
cout << msg << "\n";
}
}

bool GetEntry(Customer* customer)
{
prompt("Enter Name: ");
cin.getline(customer->Name, NAME_MAX);
prompt("Enter Address: ");
cin.getline(customer->Address, ADDR_MAX);
prompt("Enter Phone Number: ");
cin >> customer->PhoneNo;
prompt("Enter D.O.B: ");
cin >> customer->DOB;

bool r_val;
cin >> rVal;

}

int main()
{
while(true)
{
Customer c;
if(!GetEntry(&c))
break;
}
}

return r_val;
}




[Edited by - staticVoid2 on October 24, 2008 4:48:26 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!