942,967 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 181
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 8th, 2010
0

array of structures, functions, and looping

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. //************************************************************************************
  2. //
  3. // This program keeps track of speakers' bureau.
  4. // It displays a menu, where the user can add info,
  5. // view the info of another speaker that's already in there,
  6. // and etc.
  7. //*************************************************************************************
  8.  
  9. #include "stdafx.h"
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <cstdlib>
  13. using namespace std;
  14.  
  15. //**************************************************************************************
  16. // function prototypes
  17. //**************************************************************************************
  18. void displayMenu();
  19. int getChoice(int);
  20. void addSpeaker(Speaker&);
  21. void viewSpeaker();
  22. void showCount();
  23.  
  24. //**************************************************************************************
  25. // this is the Speaker structure declaration
  26. //**************************************************************************************
  27. struct Speaker{
  28. char name[128],
  29. telNumber[128],
  30. topic[128];
  31. double fee;
  32.  
  33. };
  34.  
  35. //**************************************************************************************
  36. // this function displays the menu to the user
  37. //**************************************************************************************
  38. void displayMenu(){
  39. cout << "\n\tWelcome to Speaker's Bureau Menu\n\n";
  40. cout << "1. Add a speaker\n";
  41. cout << "2. View speaker's info\n";
  42. cout << "3. Display the number of speakers that are on the list\n";
  43. cout << "4. Quit the program\n\n";
  44. return;
  45. }
  46.  
  47. //**************************************************************************************
  48. // this function inputs, validates and returns the user's menu choices
  49. //**************************************************************************************
  50. int getChoice(){
  51. int choice;
  52. cin >> choice;
  53. while (choice < 1 || choice > 4){
  54. cout << "Please, enter a valid choice number, 1-4: ";
  55. cin >> choice;
  56. };
  57. return choice;
  58. }
  59.  
  60. //**************************************************************************************
  61. // this function lets the user add info/data to any speaker that's chosen
  62. //**************************************************************************************
  63. void addSpeaker(Speaker &sp){ //parameter issue &sp ???
  64. int index = 0;
  65. cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
  66. cin >> index;
  67. if (index == 1){ //isn't there an easier/shorter way of inputting, instead of this long if/else???
  68. cout << "Name: ";
  69. cin >> sp[0].name >> endl;
  70. cout << "Telephone Number: ";
  71. cin >> sp[0].telNumber >> endl;
  72. cout << "Topic: ";
  73. cin >> sp[0].topic << endl;
  74. cout << "Required fee: ";
  75. cin >> sp[0].fee >> endl;
  76. };
  77. else if (index == 2){
  78. cout << "Name: ";
  79. cin >> sp[1].name >> endl;
  80. cout << "Telephone Number: ";
  81. cin >> sp[1].telNumber >> endl;
  82. cout << "Topic: ";
  83. cin >> sp[1].topic << endl;
  84. cout << "Required fee: ";
  85. cin >> sp[1].fee >> endl;
  86. };
  87. else if (index == 3){
  88. cout << "Name: ";
  89. cin >> sp[2].name >> endl;
  90. cout << "Telephone Number: ";
  91. cin >> sp[2].telNumber >> endl;
  92. cout << "Topic: ";
  93. cin >> sp[2].topic << endl;
  94. cout << "Required fee: ";
  95. cin >> sp[2].fee >> endl;
  96. };
  97. else if (index == 4){
  98. cout << "Name: ";
  99. cin >> sp[3].name >> endl;
  100. cout << "Telephone Number: ";
  101. cin >> sp[3].telNumber >> endl;
  102. cout << "Topic: ";
  103. cin >> sp[3].topic << endl;
  104. cout << "Required fee: ";
  105. cin >> sp[3].fee >> endl;
  106. };
  107. else if (index == 5){
  108. cout << "Name: ";
  109. cin >> sp[4].name >> endl;
  110. cout << "Telephone Number: ";
  111. cin >> sp[4].telNumber >> endl;
  112. cout << "Topic: ";
  113. cin >> sp[4].topic << endl;
  114. cout << "Required fee: ";
  115. cin >> sp[4].fee >> endl;
  116. };
  117. else if (index == 6){
  118. cout << "Name: ";
  119. cin >> sp[5].name >> endl;
  120. cout << "Telephone Number: ";
  121. cin >> sp[5].telNumber >> endl;
  122. cout << "Topic: ";
  123. cin >> sp[5].topic << endl;
  124. cout << "Required fee: ";
  125. cin >> sp[5].fee >> endl;
  126. };
  127. else if (index == 7){
  128. cout << "Name: ";
  129. cin >> sp[6].name >> endl;
  130. cout << "Telephone Number: ";
  131. cin >> sp[6].telNumber >> endl;
  132. cout << "Topic: ";
  133. cin >> sp[6].topic << endl;
  134. cout << "Required fee: ";
  135. cin >> sp[6].fee >> endl;
  136. };
  137. else if (index == 8){
  138. cout << "Name: ";
  139. cin >> sp[7].name >> endl;
  140. cout << "Telephone Number: ";
  141. cin >> sp[7].telNumber >> endl;
  142. cout << "Topic: ";
  143. cin >> sp[7].topic << endl;
  144. cout << "Required fee: ";
  145. cin >> sp[7].fee >> endl;
  146. };
  147. else if (index == 9){
  148. cout << "Name: ";
  149. cin >> sp[8].name >> endl;
  150. cout << "Telephone Number: ";
  151. cin >> sp[8].telNumber >> endl;
  152. cout << "Topic: ";
  153. cin >> sp[8].topic << endl;
  154. cout << "Required fee: ";
  155. cin >> sp[8].fee >> endl;
  156. };
  157. else if (index == 10){
  158. cout << "Name: ";
  159. cin >> sp[9].name >> endl;
  160. cout << "Telephone Number: ";
  161. cin >> sp[9].telNumber >> endl;
  162. cout << "Topic: ";
  163. cin >> sp[9].topic << endl;
  164. cout << "Required fee: ";
  165. cin >> sp[9].fee >> endl;
  166. };
  167. else {
  168. cout << "Please, enter a valid index number: ";
  169. };
  170.  
  171. return;
  172. }
  173.  
  174. //**************************************************************************************
  175. // this function lets the user see the info of a chosen speaker
  176. //**************************************************************************************
  177. void viewSpeaker(Speaker sp){
  178. cout << "Please, choose the speaker's index, 1-10, "
  179. << "that you are interested in, and it will be displayed: ";
  180. cin >> index;
  181. if (index == 1){
  182. cout << sp[0].name << " - " << sp[0].telNumber << " - " << sp[0].topic << emdl;
  183. };
  184. else if (index == 2){
  185. cout << sp[1].name << " - " << sp[1].telNumber << " - " << sp[1].topic << endl;
  186. };
  187. else if (index == 3){
  188. cout << sp[2].name << " - " << sp[2].telNumber << " - " << sp[2].topic << endl;
  189. };
  190. else if (index == 4){
  191. cout << sp[3].name << " - " << sp[3].telNumber << " - " << sp[3].topic << endl;
  192. };
  193. else if (index == 5){
  194. cout << sp[4].name << " - " << sp[4].telNumber << " - " << sp[4].topic << endl;
  195. };
  196. else if (index == 6){
  197. cout << sp[5].name << " - " << sp[5].telNumber << " - " << sp[5].topic << endl;
  198. };
  199. else if (index == 7){
  200. cout << sp[6].name << " - " << sp[6].telNumber << " - " << sp[6].topic << endl;
  201. };
  202. else if (index == 8){
  203. cout << sp[7].name << " - " << sp[7].telNumber << " - " << sp[7].topic << endl;
  204. };
  205. else if (index == 9){
  206. cout << sp[8].name << " - " << sp[8].telNumber << " - " << sp[8].topic << endl;
  207. };
  208. else if (index == 10){
  209. cout << sp[9].name << " - " << sp[9].telNumber << " - " << sp[9].topic << endl;
  210. };
  211. else {
  212. cout << "Please, enter a valid index number: ";
  213. };
  214. return;
  215. }
  216.  
  217. //**************************************************************************************
  218. // main function
  219. //**************************************************************************************
  220. int main(){
  221.  
  222. struct Speaker sp[10] = {{"_", "_", "_", 0},
  223. {"_", "_", "_", 0},
  224. {"_", "_", "_", 0},
  225. {"_", "_", "_", 0},
  226. {"_", "_", "_", 0},
  227. {"_", "_", "_", 0},
  228. {"_", "_", "_", 0},
  229. {"_", "_", "_", 0},
  230. {"_", "_", "_", 0},
  231. {"_", "_", "_", 0}}; //info spaces for 10 speakers that are ready to be filled
  232.  
  233. do{
  234. int choice;
  235. displayMenu();
  236. choice = getChoice();
  237.  
  238. if(choice == 1){
  239. addSpeaker();
  240. };
  241. else if(choice == 2){
  242. viewSpeaker();
  243. };
  244. else if(choice == 3){
  245. showCount()
  246. };
  247. else if(choice == 4){
  248. cout << "Thanks for using my app. Bye.";
  249. exit(0);
  250. };
  251. }while (choice != 0);
  252.  
  253.  
  254. return 0;
  255. }
  256.  
  257.  
  258. ////////////////////////////////////////////////////////////////////////////////////
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
Last edited by Nick Evan; Sep 8th, 2010 at 1:37 pm. Reason: Added CODE-tags, do get rid of the "dung" as Fbody put it.. :)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
super-duper is offline Offline
19 posts
since Sep 2010
Sep 8th, 2010
1
Re: array of structures, functions, and looping
First, your code is nearly impossible to read, in the future use [CODE]...code tags...[/CODE], then we can help easier.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. int main() {
  3. std::cout << "This looks nice, and is easily readable." << std::endl;
  4. return 0;
  5. }

#include <iostream>
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?
C++ Syntax (Toggle Plain Text)
  1. const int COUNT = 5;
  2. int myArray[COUNT] = [0, 1, 2, 3, 4 };
  3.  
  4. void displayInfo(int requestedItem) {
  5. if (requestedSize < COUNT)
  6. cout << myArray[requestedItem - 1] << endl;
  7. else
  8. cout << "You have requested an invalid element." << endl;
  9. }
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 8th, 2010
0
Re: array of structures, functions, and looping
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
super-duper is offline Offline
19 posts
since Sep 2010
Sep 8th, 2010
1
Re: array of structures, functions, and looping
I'm quite sure Fbody's post will do what you want - he's just not going to do everything for you.

e.g.,
c++ Syntax (Toggle Plain Text)
  1. const int COUNT = 5;
  2. Speaker myArray[COUNT] = [0, 1, 2, 3, 4 };
  3.  
  4. void displayInfo(int requestedItem) {
  5. if (requestedSize < COUNT)
  6. cout << myArray[requestedItem - 1].showName();
  7. etc.
  8. etc.
  9. else
  10. cout << "You have requested an invalid element." << endl;
  11. }

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 for every element
if() - Do this for the selected element

What I would suggest is remove your if()'s and try using switch()
Last edited by Duki; Sep 8th, 2010 at 1:01 pm.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Sep 8th, 2010
0
Re: array of structures, functions, and looping
>>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):
C++ Syntax (Toggle Plain Text)
  1. const int COUNT = 5;
  2. struct Speaker mySpeakers[COUNT];
  3.  
  4. void displayInfo(int requestedItem) {
  5. if (requestedItem <= COUNT)
  6. cout << myArray[requestedItem - 1].someFunction();
  7. else
  8. cout << "You have requested info on a non-existent speaker." << endl;
  9. }
Do you still think it won't work?

You would do something similar with addSpeaker().
Last edited by Fbody; Sep 8th, 2010 at 1:25 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 8th, 2010
0
Re: array of structures, functions, and looping
Can't you just get rid of all the if statements, and use
C++ Syntax (Toggle Plain Text)
  1. cin >> sp[index - 1].name >> endl;
  2. //etc ...

And then the same concept for the viewSpeaker?
Reputation Points: 48
Solved Threads: 30
Practically a Master Poster
kes166 is offline Offline
632 posts
since Aug 2010
Sep 8th, 2010
0
Re: array of structures, functions, and looping
Quote originally posted by kes166; ...
Can't you just get rid of all the if statements, and use
C++ Syntax (Toggle Plain Text)
  1. cin >> sp[index - 1].name >> endl;
  2. //etc ...

And then the same concept for the viewSpeaker?
What would happen if index <= 1 ?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 8th, 2010
0
Re: array of structures, functions, and looping
Click to Expand / Collapse  Quote originally posted by Nick Evan ...
What would happen if index <= 1 ?
C++ Syntax (Toggle Plain Text)
  1. cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
  2. 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
C++ Syntax (Toggle Plain Text)
  1. while (choice < 1 || choice > 4){
  2. cout << "Please, enter a valid choice number, 1-4: ";
  3. cin >> choice;
Last edited by kes166; Sep 8th, 2010 at 1:48 pm.
Reputation Points: 48
Solved Threads: 30
Practically a Master Poster
kes166 is offline Offline
632 posts
since Aug 2010
Sep 8th, 2010
0
Re: array of structures, functions, and looping
Thank you very much, guys. I really appreciate it. I think i got that [index-1] part.
but, one more question about getline function.
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. using namespace std;
  7. ...........
  8. ...........
  9. void addSpeaker(Speaker sp[]){
  10. int index = 0;
  11. int i;
  12.  
  13. do{
  14. cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
  15. cin >> index;
  16. cout << "Name: ";
  17. getline(cin, sp[index - 1].name);
  18. cout << "Telephone Number: ";
  19. getline(cin, sp[index - 1].telNumber);
  20. cout << "Topic: ";
  21. getline(cin, sp[index - 1].topic);
  22. cout << "Required fee: ";
  23. getline(cin, sp[index - 1].fee);
  24. }while(index !<1 || index !> 10);
  25.  
  26. return;
  27. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
super-duper is offline Offline
19 posts
since Sep 2010
Sep 8th, 2010
0
Re: array of structures, functions, and looping
i didnt post the whole program here. it's pretty long.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
super-duper is offline Offline
19 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: encryptor/decryptor using ASCII table
Next Thread in C++ Forum Timeline: Hi I really needed help for this number guessing game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC