ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a programm in C++ that mimics a database, and this problem keeps comming up, and I can't seem to get rid of it. I placed a comment above the line where the problem is. And sorry its so long!
Code:
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <stdio.h>
#include <iomanip>
using namespace std;
struct Score
{ // making record fields
char student[50];
char studentscore[50];
char student_name;
int how_many;
};
.
.
.
// Add score function
void add_Score(int&, Score [])
{
//declareing local varables that are needed
char score[50];
char studentscore[50] ;
char student_name;
int student_score ;
int how_many;
int new_score;
int i = -1 ;
int num_students;
char choice = 'Y' ;
Score The_Scores[50];
cout << "You selected 'Add a score' to the list." << endl ;
cout << endl ;
// user enters a score which is stored in score[20])
cout << "\t"<< "1) Enter the score (up to 3 characters) "
<< endl << "\t" << ": " ;
fflush(stdin);
gets(score) ;
//searches for the student name the user enters
//the following line is where the problem is
while (strcmp(score, The_Scores[i].student_name)!=0 && i <=num_students)
i++ ;
//if the student name was not found
if (strcmp(score, The_Scores[i].student_name)!=0)
{
cout << "The student name was not found and will be added to the list." << endl ;
cout << "\n\t" << "2) Please, enter the student's last name (up to 20 characters)"
<< endl << "\t" << ": " ;
fflush(stdin) ;
gets(studentscore) ;
cout << "\n\t" << "3) Now, enter the enter the students score: "
<< endl << "\t" << ": " ;
fflush(stdin) ;
gets(score) ;
// the info is then stored in the array 'Scores'
strcpy(The_Scores[num_students].student, score);
strcpy(The_Scores[num_students].studentscore, score);
strcpy(The_Scores[num_students].student_name, student_name) ;
The_Scores[num_students].studentscore = studentscore ;
The_Scores[num_students].how_many = how_many;
num_students++; // increment after adding another score
}
// if there is a match with a student's name already in inventory, user is notified
//if sutdents name is there option to update score is given
{
cout << setw(59) << "The student's name is already in the list." << endl ;
cout << setw(70) << "Do you want to update the his/her Score (Yes/No)? " ;
cin >> choice ;
cout << endl ;
if (choice == 'Y' || choice == 'y')
{
cout << "Enter students new score: " ;
fflush(stdin) ;
cin >> new_score ;
The_Scores[i].student_name, score = The_Scores[i].student_name, new_score ; //not sure if its right
}
}
cout << endl ;
} //end of function
My advice, would be not to use char*, but use the string class from the STL. This would cure most of your problems. you've included the header why not use it?
if ( score != The_Scores[i].student_name && i <=num_students )
char is a single character, ( as in studentname ) char* is a pointer to a char. arrays of chars are used for strings. a char array without an index is also a char*.
string is a c++ class. Its very useful, as unlike a char array, its dynamically sized ( no need to worry so much about its length. ) and can beused with operators.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.