Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


C++ Help

  • Please log in to reply
1 reply to this topic

#1 locally pwned

locally pwned

  • Members
  • 489 posts
  • Gender:Male
  • Location:Portland, Oregon
  • Local time:08:08 AM

Posted 27 January 2007 - 08:04 PM

Hey folks, I am taking the second C++ programing class this term, and I have a problem. Here's a snippet of the code. I have removed the functions not pertinent to the snag to save space on this post. The problem is getting a command from the user and applying it in the while loop there in the main() function.

#include <iostream>
using namespace std;

const int MAX_CHAR = 100;

struct Food
char foodName[MAX_CHAR]; //yup, name of each food
int category; //meat, grain, ect...type of food
float calories;
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams

void displayMenu(); //menu function
char getCom(); //get command function
void exeCom(Food pile[], int& size, char cmd); //execute command function

int main()
char command;
Food foodPile[100];
int size = 0;
command = getCom(); <--- here's where the program dies!

// cout << "You chose: " << command << endl; // test line

while (command !='q')
exeCom(foodPile, size, command);
command = getCom();
return 0;

void displayMenu()
cout << "Please select one of the following options:" << endl;
cout << endl << "a: Add a food to the database." << endl
<< "l: List the foods already in the database." << endl
<< "d: Delete a food from the database." << endl
<< "q: Exit the program." << endl;

char getCom()
char choice;

cout << "Please enter your choice (a, l, d, or q): ";
cin >> choice;
cin.ignore(100, '/n');

return choice;

void exeCom(Food pile[], int& size, char cmd)
Food aFood;
switch (cmd)
case 'a':
addFood(pile, size, aFood);
case 'l':
void showList(list, size);
case 'd': //where the delete function will go
cout <<"Sorry, that's not a valid option." << endl;
cout <<"Please make another selection: ";

The problem is in main(), were the function char getCom() doesn't seem to "get" a value for command, so command = getCom() doesn't do anything and the program just sits there.

Any help would be much appreciated!
"The whole of science is nothing more than a refinement of everyday thinking." - Albert Einstein

"The world is my country, all mankind are my brethren, and to do good is my religion." - Thomas Paine

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands." - Douglas Adams

BC AdBot (Login to Remove)


#2 jv16


  • Members
  • 1 posts
  • Local time:11:08 AM

Posted 30 January 2007 - 04:39 PM

Well, the problem lies in the "cin.ignore(100, '/n');" line, it discards the input buffer. Simply remove that line and the code should work perfectly.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users