473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

C++ Switch Statement within program to perform multiple functions

Here is the code for my program
Expand|Select|Wrap|Line Numbers
  1. // Book Price (16800960)
  2.  
  3.  
  4.   #include <iostream>
  5.   #include <string>
  6.  #include <iomanip>
  7.  
  8.  const int MAXSIZE 300;
  9.  
  10.  using namespace std;
  11.  void displayMenu ();
  12.  void listRecords(string allTitles[300], double allPrices[300], int totalRec);
  13.  
  14.  string allTitles[MAXSIZE] = {
  15.         "Book 1",
  16.         "Book 2"
  17.     };
  18.     double allPrices[MAXSIZE] = {
  19.         78.5, 66.
  20.     };
  21.          int main ()
  22.  
  23.          {
  24.  
  25.  void displayMenu ();
  26.  
  27.  
  28.  
  29.  cout << "MAIN MENU"<<"\n";cout<<endl;
  30.  cout << " 0. Exit " " 1. Enter Book " " 2. Find Book "<<"\n"; cout <<endl;
  31.  cout << " 3. List All "  " 4. Delete Book"   " 5. Update Book " << "\n"; 
  32.  cout   <<endl;
  33.  
  34.        string Title;
  35.        double Price; 
  36.        int totalRec =2;
  37.        char menu;
  38.        cout << "Please Enter Menu Selection" ;
  39.         cin >> menu; 
  40.         while (menu!=0);
  41.         switch (menu)
  42.  
  43.        {
  44.  
  45.          case '4':
  46.              string title;
  47.      cout << "Enter Title of the book record to delete ->";
  48.  
  49.     getline(cin,title);
  50.    for(int i = 0; i < totalRec; i++)
  51.      {
  52.       if (title == allTitles[i])
  53.       {allTitles[i] = allTitles[i--];
  54.       allPrices[i] = allPrices[i--];
  55.        totalRec = (totalRec --);
  56.       cout << " Record Erased Successfully"; cout <<endl;
  57.  
  58.       }}

The compiler is not showing errors but i want to see if the function i have written in case 4 will accept a title check against the array if it finds a match delete the record and the corresponding price


as the program is not loading once a number is inputted basically it need it to perform the delete function if a matching title is found


now it is the if loop and array declaration that i believe i have got wrong

Any Help on this would be greatly appreciated
Jun 3 '10 #1
9 2919
Dheeraj Joshi
1,123 Expert 1GB
Is it incomplete code or you forgot to declare your main function?

Regards
Dheeraj Joshi
Jun 3 '10 #2
Dheeraj Joshi
1,123 Expert 1GB
Also what is the logic behind following codes?

Expand|Select|Wrap|Line Numbers
  1. if (title == allTitles[i]){
  2. allTitles[i] = allTitles[i--];
  3. allPrices[i] = allPrices[i--];
  4.  
Regards
Dheeraj Joshi
Jun 3 '10 #3
Banfa
9,065 Expert Mod 8TB
@16800960
now it is the if loop and array declaration that i believe i have got wrong
Good spot.

The first issue, book title and price are related on a 1 - 1 basis. However you have implemented them as 2 separate things in 2 separate arrays. A book is an object that has many attributes, you should implement it as such using a class. If your book currently only has title and price but is implemented in a working program as a class then it is fairly easy to add more attributes, author say, by altering the class. Currently to add a new attribute you would need to add a new array and alter most of the code.

1. Implement book as a class

Your for loop does not handle the array correctly, it only acts on one entry in the array but in reality it would need to act of all entries following the erased entry. However in C++ there is a better way than arrays which are not managed and have little functionality. In C++ there are the STL containers, they manage memory for you and provide additional functionality. Using a container you would just have to find the correct record and then call the erase function. For an application like this I would recommend a <list>.

This will greatly simply your code.

2. Don't use arrays use STL containers
Jun 3 '10 #4
the logic behind the -- functions though they are not declared correctly was if a matching title is found it is meant to remove the record from the arrays the reason two arrays are used is that the program specifies that the two arrays must be present and executed before the menu appears


i had a better for loop for the other section though i would use a list as long as i can point it to the arrays cannot use classes unfortunately i would have used a struct normally though this doesn't need the extra attributes though they would be a nice feature not required by program specifications


if you can see an easy way to resolve this issue then i would be most greatful as i have limited time to get this done mainly have to deal with case 4 and the menu not returning causing the lockup of the program.


as far as main goes i should have closed it earlier after the menu is called i mispositioned the closing bracket
Jun 3 '10 #5
Banfa
9,065 Expert Mod 8TB
OK I thought you might say something like that.

So try this, if you start with an array of book titles

{"Book 1", "Book 2", "Book 3", "Book 4", "Book 5"}
size = 5

And you want to remove "Book 2" from the array so it looks like this

{"Book 1", "Book 3", "Book 4", "Book 5"}
size = 4

Remembering that arrays do not actually have an erase function what operations do you have to perform to transform the first list to the second?
Jun 3 '10 #6
first function is to search the array for the matching title

then select the position of the match then remove that position and update the total rec


Then the Price is done similarly though it selects based on the psoiton of the title though

really i would

search for string title " book2"

use a for loop which will show

the position of alltitles[i]

then maybe use a where statement where pos of alltitles = pos of allprices delete
?

delete [] Alltitles where string title = alltitles [i] or maybe memset ()

im not sure as i haven't done a delete before
Jun 3 '10 #7
Banfa
9,065 Expert Mod 8TB
OK and once you have found the position of the book to remove, what is it you actually do to remove it?
Jun 3 '10 #8
Dheeraj Joshi
1,123 Expert 1GB
Hi 16800960.
Arrays may not be best option to solve your problem.
Is there any specification saying you need to achieve it using arrays?
If not go for Templates. Banfa already mentioned about <list>

Regards
Dheeraj Joshi
Jun 3 '10 #9
the specifications Dheera is that the data is stored in the arrays not that i have to use the arrays to delete though that was i what i was assuming as it would be easiest.

as far as the delete wouldn't i have to find the element which matches the getline string then set element = number 1 lower and apply this to all larger elements

something similar to this maybe

for ( i = 0; i < n; i++ ) {

if ( alltitles[i] == target )

break;

while ( ++i < n )

a[i - 1] = a[i];

--n;
Jun 3 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Skip Montanaro | last post by:
Stephen> { Stephen> 'one': lambda x:x.blat(), Stephen> 'two': lambda x:x.blah(), Stephen> }.get(someValue, lambda x:0)(someOtherValue) One thing to remember is that function calls in Python...
26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
24
by: Mark | last post by:
Hi - how can I get a switch statement to look for a range of values? I have: function payCalc(field, field2) switch(field.value) { case 0-50: field2.value="lower band"; case 51-99:...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
3
by: mattyizzo | last post by:
Here is my code. Basically I'm trying to solve for CI and CF, where each use a bunch of constants and a variable or two which can have up to 10 values. I get the following error, however, at line...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.