473,386 Members | 1,779 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,386 software developers and data experts.

Having trouble editing a record from a list of students

Hi, I've been trying to get this function to work(editing a record from a list of students) and I've been having trouble with it.. can someone help me with it, thank you

Expand|Select|Wrap|Line Numbers
  1. int Search(Student List[], int Size, string Target)
  2. {
  3.     for(int i = 0 ; i < Size ; i++)
  4.         if (Target == List[i].LastName)
  5.             return i;
  6.     return -1;
  7. }
  8.  
  9. void Edit_Record(Student List[], Student & Temp)
  10. {
  11.     int Index;
  12.     string Target;
  13.     int Size = 0;
  14.  
  15.     cout << "Please enter the student Last Name you want to edit: ";
  16.     cin >> Target;
  17.     Index = Search(List,Size,Target);
  18.     if(Index!=-1){
  19.         cout<<"Procede to change the student's information"<<endl;
  20.  
  21.         cout << "Enter Student" << List[Index].LastName + ", " + List[Index].FirstName << "'s new information" << endl;
  22.         cout << "Enter the new First Name: ";
  23.         cin >> List[Index].FirstName;
  24.         cout << "Enter the new Last Name: ";
  25.         cin >> List[Index].LastName;
  26.         cout << "Enter the new Quiz Score: ";
  27.         cin >> List[Index].Quiz;
  28.             cout << setw(20) <<"Scores for Six Tests:" << endl ;
  29.             for (int j = 0 ; j < 6 ; j++)
  30.             {
  31.                 cout <<setw(7)<< " Test # " << j+1 << ": " ;
  32.                 cin >> List[Index].Test[j];
  33.  
  34.             }
  35.     }
  36.     else
  37.         cout << "\nThis student information does not exist on the list. Try again.\n";
  38.     List[Index] = Temp;
  39. }
  40.  
Dec 8 '13 #1
1 1098
weaknessforcats
9,208 Expert Mod 8TB
First, this code:
Expand|Select|Wrap|Line Numbers
  1. cin >> Target;
limits the last name to one word. If there can be last names of more than one word (weaknessforcats III) then this code will crash the program. You would use cin.getline() instead.

Second, this code:
Expand|Select|Wrap|Line Numbers
  1. Index = Search(List,Size,Target);
passes Size to Search. Size is defined as 0 so the Search probably won't look at much.

The thing about arrays is once you pass the array to a function all that passes is the address of the array so that looking from inside the function you see the address but not the number of elements. If you use an array, then you must always pass in the number of elements in addition to the array name.

Third, your Search command should return 0 for success and not-zero for failure. The programming community is used t this. The not-zero allows you to return many values from a function so the nature of the error can be better handles by the calling function.

Fourth, this code:
Expand|Select|Wrap|Line Numbers
  1. cin >> Target;
only works if the last name is just a single word. weaknessforcats III would not be allowed.

Read up on cin. Every function using cin, like the operator>> in this case, first checks to see if the stream is in an error state, If it is, then operator>> just returns without doing anything. The effect you see is that things go along and then suddenly every cin>> in the program stops working.

You should have code like:

Expand|Select|Wrap|Line Numbers
  1. cin >> Target;
  2. if (cin.fail())
  3. {
  4.      //data entry failed
  5.      //return an error code
  6. }
  7.  
The is also a cin.good() and the one you use is the one that does not require an ! in the code. Like, if (!cin.fail()). Use if (cin.good()) instead.

You can clear the stream failbit by calling cin.clear(). But note that this just resets the failbit and does not fix the data problem that caused the failbit to be set.

I hope some of this helps.
Dec 8 '13 #2

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

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
2
by: ed | last post by:
i'm having trouble with a form. I want to be able to type in the address of the form with the data for the form items in the URL (ie: http://somesite.com/formpage.html?field1=data1&field2=data2)....
2
by: Jake Barnes | last post by:
I've read over the documentation for these effects: http://wiki.script.aculo.us/scriptaculous/show/CombinationEffectsDemo I want to include them on my page. I tried attaching using onload, but...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
5
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL=...
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
12
by: Fozzi | last post by:
Hey all I have been working on a project which allows me to write to a file and retrieve from that file, but i having trouble with it These are my inputs: private int flightno; ...
1
by: Alexnb | last post by:
Okay, what I want to do with this code is to got to thesaurus.reference.com and then search for a word and get the syns for it. Now, I can get the syns, but they are still in html form and some are...
1
by: BarbQb | last post by:
Hi All, I am having trouble filtering a report with a multiselect list box. The report is generated, but it is not filtered based on the selections of the list box: lstVehID Multi Select...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.