954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

array of structures, functions, and looping

//************************************************************************************
//
//			This program keeps track of speakers' bureau.
//			It displays a menu, where the user can add info,
//			view the info of another speaker that's already in there,
//			and etc.
//*************************************************************************************

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

//**************************************************************************************
// function prototypes
//**************************************************************************************
void displayMenu();
int getChoice(int);
void addSpeaker(Speaker&);
void viewSpeaker();
void showCount();

//**************************************************************************************
// this is the Speaker structure declaration
//**************************************************************************************
struct Speaker{
	char name[128],
		 telNumber[128],
		 topic[128];
	double fee;

};

//**************************************************************************************
// this function displays the menu to the user
//**************************************************************************************
void displayMenu(){
	cout << "\n\tWelcome to Speaker's Bureau Menu\n\n";
	cout << "1. Add a speaker\n";
	cout << "2. View speaker's info\n";
	cout << "3. Display the number of speakers that are on the list\n";
	cout << "4. Quit the program\n\n";
	return;
}

//**************************************************************************************
// this function inputs, validates and returns the user's menu choices
//**************************************************************************************
int getChoice(){
	int choice;
	cin >> choice;
	while (choice < 1 || choice > 4){
		cout << "Please, enter a valid choice number, 1-4: ";
		cin  >> choice;
	};
	return choice;
}

//**************************************************************************************
// this function lets the user add info/data to any speaker that's chosen
//**************************************************************************************
void addSpeaker(Speaker &sp){       //parameter issue &sp ???
	int index = 0;
	cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
	cin  >> index;	
	if (index == 1){	//isn't there an easier/shorter way of inputting, instead of this long if/else???
		cout << "Name: ";
		cin  >> sp[0].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[0].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[0].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[0].fee >> endl;
	};
	else if (index == 2){
		cout << "Name: ";
		cin  >> sp[1].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[1].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[1].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[1].fee >> endl;
	};
	else if (index == 3){
		cout << "Name: ";
		cin  >> sp[2].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[2].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[2].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[2].fee >> endl;
	};
	else if (index == 4){
		cout << "Name: ";
		cin  >> sp[3].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[3].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[3].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[3].fee >> endl;
	};
	else if (index == 5){
		cout << "Name: ";
		cin  >> sp[4].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[4].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[4].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[4].fee >> endl;
	};
	else if (index == 6){
		cout << "Name: ";
		cin  >> sp[5].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[5].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[5].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[5].fee >> endl;
	};
	else if (index == 7){
		cout << "Name: ";
		cin  >> sp[6].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[6].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[6].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[6].fee >> endl;
	};
	else if (index == 8){
		cout << "Name: ";
		cin  >> sp[7].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[7].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[7].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[7].fee >> endl;
	};
	else if (index == 9){
		cout << "Name: ";
		cin  >> sp[8].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[8].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[8].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[8].fee >> endl;
	};
	else if (index == 10){
		cout << "Name: ";
		cin  >> sp[9].name >> endl;
		cout << "Telephone Number: ";
		cin  >> sp[9].telNumber >> endl;
		cout << "Topic: ";
		cin  >> sp[9].topic << endl;
		cout << "Required fee: ";
		cin  >> sp[9].fee >> endl;
	};
	else {
		cout << "Please, enter a valid index number: ";
	};
	
	return;
}

//**************************************************************************************
// this function lets the user see the info of a chosen speaker
//**************************************************************************************
void viewSpeaker(Speaker sp){
	cout << "Please, choose the speaker's index, 1-10, "
		 << "that you are interested in, and it will be displayed: ";
	cin  >> index;
	if (index == 1){
		cout << sp[0].name << " - " << sp[0].telNumber << " - " << sp[0].topic << emdl;
	};
	else if (index == 2){
		cout << sp[1].name << " - " << sp[1].telNumber << " - " << sp[1].topic << endl;
	};
	else if (index == 3){
		cout << sp[2].name << " - " << sp[2].telNumber << " - " << sp[2].topic << endl;
	};
	else if (index == 4){
		cout << sp[3].name << " - " << sp[3].telNumber << " - " << sp[3].topic << endl;
	};
	else if (index == 5){
		cout << sp[4].name << " - " << sp[4].telNumber << " - " << sp[4].topic << endl;
	};
	else if (index == 6){
		cout << sp[5].name << " - " << sp[5].telNumber << " - " << sp[5].topic << endl;
	};
	else if (index == 7){
		cout << sp[6].name << " - " << sp[6].telNumber << " - " << sp[6].topic << endl;
	};
	else if (index == 8){
		cout << sp[7].name << " - " << sp[7].telNumber << " - " << sp[7].topic << endl;
	};
	else if (index == 9){
		cout << sp[8].name << " - " << sp[8].telNumber << " - " << sp[8].topic << endl;
	};
	else if (index == 10){
		cout << sp[9].name << " - " << sp[9].telNumber << " - " << sp[9].topic << endl;
	};
	else {
		cout << "Please, enter a valid index number: ";
	};
	return;
}

//**************************************************************************************
// main function
//**************************************************************************************
int main(){

	struct Speaker sp[10] = {{"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0},
							 {"_", "_", "_", 0}};   //info spaces for 10 speakers that are ready to be filled
	
	do{
		int choice;
		displayMenu();
		choice = getChoice();

		if(choice == 1){
			addSpeaker();  
		};
		else if(choice == 2){
			viewSpeaker();
		};
		else if(choice == 3){
			showCount()
		};
		else if(choice == 4){
			cout << "Thanks for using my app. Bye.";
			exit(0);
		};
	}while (choice != 0);
	
	
	return 0;
}


////////////////////////////////////////////////////////////////////////////////////

Hey, guys

i'm trying to write a program that basically, lets the user choose from a menu option. The menu is all the way up top. I was going fine till I hit the addSpeaker function. As you can see, i wrote down bunch of if/else and cout/cin statements. I was wondering, isn't there some other shorter way of doing it? like, when you wanna assign some number to something, you can use "for" loop and the program goes through everything and finds that spot and puts that number, and etc. You know what i mean? like, instead of writing bunch of lines of code, isn't there something shorter and simpler?

anybody?

thank you

super-duper
Light Poster
29 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

First, your code is nearly impossible to read, in the future use [CODE]...code tags...[/CODE] , then we can help easier.

#include <iostream>
int main() {
  std::cout << "This looks nice, and is easily readable." << std::endl;
  return 0;
}


#include
int main() {
std::cout << "This looks like dung, and isn't readable." << std::endl;
return 0;

So what you want to do is something like this?

const int COUNT = 5;
int myArray[COUNT] = [0, 1, 2, 3, 4 };

void displayInfo(int requestedItem) {
  if (requestedSize < COUNT)
    cout << myArray[requestedItem - 1] << endl;
  else
    cout << "You have requested an invalid element." << endl;
}
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

haha, my apologies, Fbody. I'm new to this stuff, to be honest.
back to the code: No, that was not what I had in mind.
Thanks for the help, though. I really appreciate it.

super-duper
Light Poster
29 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I'm quite sure Fbody's post will do what you want - he's just not going to do everything for you.

e.g.,

const int COUNT = 5;
Speaker myArray[COUNT] = [0, 1, 2, 3, 4 };

void displayInfo(int requestedItem) {
  if (requestedSize < COUNT)
    cout << myArray[requestedItem - 1].showName();
    etc.
    etc.
  else
    cout << "You have requested an invalid element." << endl;
}


He's merely demonstrating the usage of arrays to access your methods. What you're referring to isn't exactly what you want to do.

for() - Do this forevery element
if() - Do this for the selected element

What I would suggest is remove your if()'s and try using switch()

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

>>No, that was not what I had in mind.
Did you take the time to modify (and slightly extend, per Duki's example) it to fit your viewSpeaker() function? It should work quite nicely...

Same code, slightly modified (you still need to do some on your own though):

const int COUNT = 5;
struct Speaker mySpeakers[COUNT];

void displayInfo(int requestedItem) {
  if (requestedItem <= COUNT)
    cout << myArray[requestedItem - 1].someFunction();
  else
    cout << "You have requested info on a non-existent speaker." << endl;
}

Do you still think it won't work?

You would do something similar with addSpeaker().

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Can't you just get rid of all the if statements, and use

cin >> sp[index - 1].name >> endl;
//etc ...


And then the same concept for the viewSpeaker?

kes166
Practically a Master Poster
639 posts since Aug 2010
Reputation Points: 48
Solved Threads: 30
 

Can't you just get rid of all the if statements, and use

cin >> sp[index - 1].name >> endl;
//etc ...

And then the same concept for the viewSpeaker?

What would happen if index <= 1 ?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
What would happen if index <= 1 ?
cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
cin  >> index;


You'd need a while loop to catch if it were less than one or greater than 10. If it were one, index - 1 would be 0 which would reference the correct location in the array. If index was -2 then the while loop would catch it and ask for a new input.

Edit:

Similar to this code

while (choice < 1 || choice > 4){
cout << "Please, enter a valid choice number, 1-4: ";
cin  >> choice;
kes166
Practically a Master Poster
639 posts since Aug 2010
Reputation Points: 48
Solved Threads: 30
 

Thank you very much, guys. I really appreciate it. I think i got that [index-1] part.
but, one more question about getline function.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
...........
...........
void addSpeaker(Speaker sp[]){      
	int index = 0;
	int i;

	do{
	cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
	cin  >> index;
	cout << "Name: ";
	getline(cin, sp[index - 1].name);
	cout << "Telephone Number: ";
	getline(cin, sp[index - 1].telNumber);
	cout << "Topic: ";
	getline(cin, sp[index - 1].topic);
	cout << "Required fee: ";
	getline(cin, sp[index - 1].fee);
	}while(index !<1 || index !> 10);

	return;
}


when i try to run it, i keep getting an error message about that getline function. I think, I am using the getline in a right way. But, cant figure out what the heck is going on.

please, need some ideas :)

thanks

super-duper
Light Poster
29 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

i didnt post the whole program here. it's pretty long.

super-duper
Light Poster
29 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

i got that part too. I was supposed to use cin.getline function since I've been using c-string. Never mind. :)
i feel so dumb for not knowing such simple looking stuff, aahhhh!

super-duper
Light Poster
29 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

There are 2 different versions of getline. A globally-declared version that is used for C++ std::strings and a member function version istream::getline() . As you now know, you need the istream member version.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You