LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   invalid conversion from `char' to `const char* (https://www.linuxquestions.org/questions/programming-9/invalid-conversion-from-%60char-to-%60const-char%2A-179478/)

bru 05-08-2004 10:14 PM

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!

leonscape 05-08-2004 10:21 PM

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.

bru 05-08-2004 10:23 PM

Thanks a million leonscape!!

One of these days I'll go from trying to learn C++ to kinda knowing C++

Thanks again!

leonscape 05-08-2004 10:30 PM

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 )

bru 05-08-2004 10:54 PM

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.

leonscape 05-09-2004 12:18 PM

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.

bru 05-09-2004 03:07 PM

Ok I get it, thanks again leonscape!


All times are GMT -5. The time now is 01:44 AM.