EvilZone
Programming and Scripting => C - C++ => : Super_mario666 October 14, 2012, 09:57:30 PM
-
when ever i try to compile the code it keeps returning this error
conversion from `std::string*' to non-scalar type `std::string' requested
what exatly am i doing wrong?
heres the source.
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
void showScore(int, bool, string);
int main()
{
int count;
int numcorrect = 0;
bool rightAnswer[20];
string correctAnswers[]= {"B","D","A", "A", "C", "A", "B", "A", "C", "D"
,"B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};
string studentAnswers[20];
for(int i = 0, count = 1; i <= 19; i++, count++)
{
cout << "What is your answer to Question #" << count << "?: ";
cin >> studentAnswers[i];
while (studentAnswers[i] != "A" && studentAnswers[i] != "B" && studentAnswers[i] != "C" && studentAnswers[i] != "D")
{
cout << "Please answer in Capital letters with either A, B, C, or D.";
cin >> studentAnswers[i];
}
}
for (int n = 0; n <= 19; n++)
{
if(correctAnswers[n] == studentAnswers[n])
{
rightAnswer[n] = true;
numcorrect++;
}
else
rightAnswer[n] = false;
}
showScore(numcorrect, rightAnswer,studentAnswers);
getch();
return 0;
}
void showScore(int numcorrect, bool rightAnswer[],string studentAnswers[])
{
if (numcorrect >= 15)
{
cout << "You got " << numcorrect << " correct out of 20!!" << endl;
cout << "You passed!!" << endl;
}
else
cout << "You got " << numcorrect << " correct out of 20." << endl;
cout << "You did not pass the exam." << endl;
for (int t = 0, count = 1; t <= 19; t++, count++)
{
if(rightAnswer[t] == true)
{
cout << count << ".\t" << studentAnswers[t] << "\tCorrect" << endl;
}
else
cout << count << ".\t" << studentAnswers[t] << "\tIncorrect" << endl;
}
}
-
You haven't mentioned in which line the error occurs anyways its probably because you are using a pointer to string(from string header file) instead of using a string , might that be afunction or anything , I cant tell unless you mention in which line the error occurs
EDIT:
Tried compiling it dude you have mashed the concepts of strings with C style strings
this
string studentanswer[20];
expects you to enter STRINGS in every array element this is not a STRING , its an ARRAY OF STRINGS , the name of an array is the pointer to its first element
the function
showScore expects you to ccall it with one int , one bool and one STRING but instead you are calling it with a POINTER to STRING
change it to to
char studentasnwer[20];
and change the function definition and prototype , change the last parameter with a C style string
Edit :Instead of changing
string studentanswer[20] to char studentanswer[20]
you can simply change the prototype and header of the function to
void studentanswer(bool , int,string*); // or void studentanswer(bool,int, string[])
And you are all done
-
Instead of changing
string studentanswer[20] to char studentanswer[20]
you can simply change the prototype and header of the function to
void studentanswer(bool , int,string*); // or void studentanswer(bool,int, string[])
And you are all done
yea i tried that already before i posted. i just ended up getting another error. :o
[Linker error] undefined reference to `showScore(int, bool, std::string*)'
Also i tried to put the code from the 'showScore' function into 'main' and get rid of the function all together and it works. so its not a logical error.
Staff note: edit your posts.
-
This is probably because you have messed up with the prototype , I mean you might have forgotten to change the FUNCTION header along with function prototype, Post the ACTUAL code thats giving this error then only we can find out :/
And btw don't just get rid of function just because you can't find a solution -.- A code should be organized and thats what functions,separate file compilation and OOP does , we'll see if we could find a way through
-
You may want to change a few things with your string variables. A string is an array of characters, and you only seem to be using characters. An array of strings is a bit unnecessary, since you are dealing with single characters, for every element. You're trying to compare a user's input of answers, with the correct answers, predefined, so I think "correctAnswers" should be constant, just to ensure it doesn't change for any reason. But yeah, look at a string as an array of characters, I think this should help you look at the code differently.
For instance:
std::string sixLetters = "abcdef";
// element 1: "a", element 2: "b", ... , element 6: "f"
-
You may want to change a few things with your string variables. A string is an array of characters, and you only seem to be using characters. An array of strings is a bit unnecessary, since you are dealing with single characters, for every element. You're trying to compare a user's input of answers, with the correct answers, predefined, so I think "correctAnswers" should be constant, just to ensure it doesn't change for any reason. But yeah, look at a string as an array of characters, I think this should help you look at the code differently.
For instance:
std::string sixLetters = "abcdef";
// element 1: "a", element 2: "b", ... , element 6: "f"
Right that but that would require him to change the comparison in conditional statements too
-
well heres a revision
#include <iostream>
#include <conio.h>
using namespace std;
const char correctAnswers[20] = {'B' ,'D','A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
void showScore(int, bool, char*);
int main()
{
int count;
int numcorrect = 0;
bool rightAnswer[20];
char studentAnswers[20];
for(int i = 0, count = 1; i <= 19; i++, count++)
{
cout << "What is your answer to Question #" << count << "?: ";
cin >> studentAnswers[i];
while (studentAnswers[i] != 'A' && studentAnswers[i] != 'B' && studentAnswers[i] != 'C' && studentAnswers[i] != 'D')
{
cout << "Please answer in Capital letters with either A, B, C, or D.";
cin >> studentAnswers[i];
}
}
for (int n = 0; n <= 19; n++)
{
if(correctAnswers[n] == studentAnswers[n])
{
rightAnswer[n] = true;
numcorrect++;
}
else
rightAnswer[n] = false;
}
showScore(numcorrect, rightAnswer, studentAnswers);
getch();
return 0;
}
void showScore(int numcorrect, bool rightAnswer[], char studentAnswers[])
{
if (numcorrect >= 15)
{
cout << "You got " << numcorrect << " correct out of 20!!" << endl;
cout << "You passed!!" << endl;
}
else
cout << "You got " << numcorrect << " correct out of 20." << endl;
cout << "You did not pass the exam." << endl;
for (int t = 0, count = 1; t <= 19; t++, count++)
{
if(rightAnswer[t] == true)
{
cout << count << ".\t" << studentAnswers[t] << "\tCorrect" << endl;
}
else
cout << count << ".\t" << studentAnswers[t] << "\tIncorrect" << endl;
}
}
-
Phew this was hard to debug since the compiler wasn't telling on which line well here's the fix , I copied and pasted the functions header instead of prototype and it worked guess why ? your prototype was this
void showScore(int, bool, char*);
prototype states a function that takes one int , ONE BOOL VALUE and ONE POINTER TO CHAR or array of char
but the function definition had the header stating that it takes ONE INT , ONE POINTER TO BOOL VALUE(array of bool) and ONE POINTER TO CHAR(Array of char)
now just change the prototype to this
void showScore(int,bool*,char*);
and it will work fine
This is why I just copy paste the function header in prototype :p that does no harm , this works as a prototype well too
void showScore(int numcorrect, bool rightAnswer[], char studentAnswers[]);
cheers
-mr khan
Edit: You just need to be aware of the ERRORS your compiler gives , I mean every compiler gives a different alert for a error , DW you'll get used to it and will be good in debugging , I am getting used to it too ;)
-
Phew this was hard to debug since the compiler wasn't telling on which line well here's the fix , I copied and pasted the functions header instead of prototype and it worked guess why ? your prototype was this
void showScore(int, bool, char*);
prototype states a function that takes one int , ONE BOOL VALUE and ONE POINTER TO CHAR or array of char
but the function definition had the header stating that it takes ONE INT , ONE POINTER TO BOOL VALUE(array of bool) and ONE POINTER TO CHAR(Array of char)
now just change the prototype to this
void showScore(int,bool*,char*);
and it will work fine
This is why I just copy paste the function header in prototype :p that does no harm , this works as a prototype well too
void showScore(int numcorrect, bool rightAnswer[], char studentAnswers[]);
cheers
-mr khan
Edit: You just need to be aware of the ERRORS your compiler gives , I mean every compiler gives a different alert for a error , DW you'll get used to it and will be good in debugging , I am getting used to it too ;)
yep it works thanks alot dude ;D
-
No problem (y)
Goodluck