Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-08-2004, 10:14 PM
|
#1
|
Member
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132
Rep:
|
invalid conversion from `char' to `const char*
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
Thanks for the help!
|
|
|
05-08-2004, 10:21 PM
|
#2
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
studentname is a char, strcmp expects char*, so give it the address of student name.
Code:
while (strcmp(score, &(The_Scores[i].student_name) )!=0 && i <=num_students)
And unless all your students of single character names (you've said upto 20 ), this ain't going to work.
|
|
|
05-08-2004, 10:23 PM
|
#3
|
Member
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132
Original Poster
Rep:
|
Thanks a million leonscape!!
One of these days I'll go from trying to learn C++ to kinda knowing C++
Thanks again!
|
|
|
05-08-2004, 10:30 PM
|
#4
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
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 )
Last edited by leonscape; 05-08-2004 at 10:31 PM.
|
|
|
05-08-2004, 10:54 PM
|
#5
|
Member
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132
Original Poster
Rep:
|
Could you explain it a little more, I don't quite understand what you meen.
I'm still very new to programming, and trying to get an understanding of it all.
Last edited by bru; 05-08-2004 at 11:26 PM.
|
|
|
05-09-2004, 12:18 PM
|
#6
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
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.
eg.
Code:
string str2;
str2 = "hello world.";
if ( str2 == "hello" )
str2 += "Adding letters";
str2 = str2 + "adding letters";
No need for all the extra functions. They work as you would expect. I just think it would be easier for your code.
|
|
|
05-09-2004, 03:07 PM
|
#7
|
Member
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132
Original Poster
Rep:
|
Ok I get it, thanks again leonscape!
|
|
|
All times are GMT -5. The time now is 06:13 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|