This forum is in archive mode. You will not be able to post new content.

Author Topic: Compile error  (Read 2734 times)

0 Members and 1 Guest are viewing this topic.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Compile error
« on: October 14, 2012, 09:57:30 PM »

when ever i try to compile the code it keeps returning this error


Code: [Select]
conversion from `std::string*' to non-scalar type `std::string' requested


what exatly am i doing wrong?


heres the source.

Code: (cpp) [Select]

#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;
       
        }


}
The Bigger they are...The more likely you'll get your ass kicked

pllaybuoy

  • Guest
Re: Compile error
« Reply #1 on: October 14, 2012, 10:01:11 PM »
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
Code: [Select]
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
« Last Edit: October 15, 2012, 05:34:16 PM by ande »

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Compile error
« Reply #2 on: October 14, 2012, 10:49:34 PM »

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
Code: [Select]
[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.
« Last Edit: October 15, 2012, 04:17:23 PM by Kulverstukas »
The Bigger they are...The more likely you'll get your ass kicked

pllaybuoy

  • Guest
Re: Compile error
« Reply #3 on: October 16, 2012, 08:47:07 AM »
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
« Last Edit: October 16, 2012, 08:48:11 AM by pllaybuoy »

Offline blackboxx

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
  • Programmer, Student
    • View Profile
Re: Compile error
« Reply #4 on: October 16, 2012, 11:33:27 AM »
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:


Code: [Select]
std::string sixLetters = "abcdef";
// element 1: "a", element 2: "b", ... , element 6: "f"

pllaybuoy

  • Guest
Re: Compile error
« Reply #5 on: October 16, 2012, 12:02:14 PM »
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:


Code: [Select]
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

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Compile error
« Reply #6 on: October 16, 2012, 04:12:08 PM »

well heres a revision

Code: (cpp) [Select]

#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;


        }


}


« Last Edit: October 16, 2012, 04:12:56 PM by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

pllaybuoy

  • Guest
Re: Compile error
« Reply #7 on: October 16, 2012, 04:50:39 PM »
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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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 ;)
« Last Edit: October 16, 2012, 04:54:45 PM by pllaybuoy »

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Compile error
« Reply #8 on: October 17, 2012, 01:38:39 AM »
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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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
The Bigger they are...The more likely you'll get your ass kicked

pllaybuoy

  • Guest
Re: Compile error
« Reply #9 on: October 17, 2012, 05:20:59 AM »
No problem (y)
Goodluck


 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.