LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-08-2004, 10:14 PM   #1
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Rep: Reputation: 15
Question 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!
 
Old 05-08-2004, 10:21 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 05-08-2004, 10:23 PM   #3
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Original Poster
Rep: Reputation: 15
Thanks a million leonscape!!

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

Thanks again!
 
Old 05-08-2004, 10:30 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 05-08-2004, 10:54 PM   #5
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Original Poster
Rep: Reputation: 15
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.
 
Old 05-09-2004, 12:18 PM   #6
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 05-09-2004, 03:07 PM   #7
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Original Poster
Rep: Reputation: 15
Ok I get it, thanks again leonscape!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Conversion from Sting to Char Diederick Programming 2 11-30-2005 08:00 AM
C programming - Char to Int Conversion ? indian Programming 4 09-30-2005 08:00 AM
C programming int to char conversion paranoid times Programming 15 03-17-2005 11:06 PM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM
Invalid char. 34 in exportstr for "mem lynch Linux - General 3 10-22-2001 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration