-6

I have this code, which is basically me trying to learn c++, and I can't figure out why I keep getting the two errors

error: request for member 'length' in 'cInputChar', which is of non-class type 'char [0]'

and

error: invalid conversion from 'char*' to 'char'

I think it has something to do with the way I am declaring the char variable cInputChar. The problem is definitely associated with the getChar function.

My code is below:

int getInteger(int& nSeries);
char getChar(char& cSeriesDecision, int nSeries);

int main()
{
int nSeries = 0;
char cSeriesDecision = {0};

getInteger(nSeries);

getChar(cSeriesDecision, nSeries);

return 0;
}

//The function below attempts to get an integer variable without using the '>>'    operator.
int getInteger(int& nSeries)
{

//The code below converts the entry from a string to an integer value.

string sEntry;
stringstream ssEntryStream;

 while (true)
 {
  cout << "Please enter a valid series number: ";
   getline(cin, sEntry);
   stringstream ssEntryStream(sEntry);

   //This ensures that the input string can be converted to a number, and that the series number is between 1 and 3.
   if(ssEntryStream >> nSeries && nSeries < 4 && nSeries > 0)
   {
       break;
   }
   cout << "Invalid series number, please try again." << endl;
 }
 return nSeries;
 }

 //This function tries to get a char from the user without using the '>>' operator.
 char getChar(char& cSeriesDecision, int nSeries)
 {
 char cInputChar[0];

 while (true)
 {
   cout << "You entered series number " << nSeries << "/nIs this correct? y/n: ";
   cin.getline(cInputChar, 1);

   if (cInputChar.length() == 1)
   {
     cSeriesDecision = cInputChar;
     break;
   }
  cout << "/nPlease enter a valid decision./n";
}

return cSeriesDecision;
}
Oldskool
  • 34,211
  • 7
  • 53
  • 66
fishycrakers
  • 71
  • 3
  • 10
  • 2
    Since you're self learning, try and _know_, not _think_ how something is, even if you don't know how to fix the isolated element you can still isolate it... e.g. minimise your code to the minimal needed to reproduce the error! – Grant Thomas Jan 09 '13 at 15:23
  • 3
    Please fix your question title so that it describes the programming question. – Lightness Races in Orbit Jan 09 '13 at 15:23
  • 3
    You made a `char` array of size `0`, and then you tried to `getline` into it, and then you tried to do `.length()` on it. None of these things are possible. Which book are you using? And which standard library reference? – Lightness Races in Orbit Jan 09 '13 at 15:25

1 Answers1

2
 char cInputChar[0];

Do you really need an array of size 0? You cannot have an array of size 0 in C++. It is simply not legal.

You need something like:

#define MAX_SIZE 256

char cInputChar[MAX_SIZE];

It is just better to simply use std::string instead of c-style character array.


From the discussion in comments:

@Inafune: Please pick up a good book.You do not learn any programming language by adding and removing syntax just to get code compiled. Never write even a single line of code without understanding the purpose behind it.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 5
    @Inafune: Sounds like you don't know why you wrote it; you're just randomly adding and removing syntax. This is not a good way to solve problems... – Lightness Races in Orbit Jan 09 '13 at 15:24
  • @Inafune Even if you are self-learning, i think you need a good book (as this really could not be from any book) – asheeshr Jan 09 '13 at 15:25
  • I tried it without the [0], and edited the getline to read 'getline(cin, cInputChar)' Now I get the error 'No matching function call to getline...' – fishycrakers Jan 09 '13 at 15:25
  • 3
    @Inafune: You need a chatroom (and a book). SO Q&A is not the appropriate place for iterative teaching. – Lightness Races in Orbit Jan 09 '13 at 15:26
  • @LightnessRacesinOrbit I have a book, but I'm not entirely sure what I should be doing when this particular error occurs. – fishycrakers Jan 09 '13 at 15:28
  • 1
    @Inafune: Just having a book won't suffice.Reading it would probably(provided it is a good book).Do you understand what is the purpose of `cInputChar`? If not then playing around with `0` or without won't help you much. – Alok Save Jan 09 '13 at 15:30
  • @Alok Save I was using cInputChar to take the input, and check it's size. After that, if the size was equal to 1 I'd store it in cSeriesDecision. To be fair, when posting on Q&A, I kinda expected help. I do appreciate you're input though. And I have been reading it. – fishycrakers Jan 09 '13 at 15:33
  • 1
    @Inafune: We will answer interesting questions but this isn't a "free help chat" -- you are generally expected to have learned the language you're asking about. We can't do that for you. It may not feel like it to you now, but we _are_ helping you in the best possible way. – Lightness Races in Orbit Jan 09 '13 at 15:40
  • @lightness Races in Orbit: I understand. By overcoming the problem myself I will learn more. I'll do some more reading. Char just seems like the best variable type; as a sting isn't necessary. The input is either 'y' or 'n', so a string would be pointless. I got rid of the char array, like Alok suggested, but kept it as a char type. Now I'm just getting a getline error. Thanks for all the help anyway. I do appreciate it. – fishycrakers Jan 09 '13 at 15:45
  • @Inafune: Well think of it this way -- your code doesn't know how long a line is going to be, so how can you fit a whole line into a single `char`? – Lightness Races in Orbit Jan 09 '13 at 15:53
  • @LightnessRacesinOrbit: That makes more sense. So a string is needed for the input, and the length can be checked afterward. But a string is essentially a character array, right? So it needs to be altered with a pointer, I think. God, I really need to read some more before I attempt this. – fishycrakers Jan 09 '13 at 16:32
  • @Inafune: Or use a stream extraction method other than `getline`. No pointers required. Yes, you definitely need to read a lot more. – Lightness Races in Orbit Jan 09 '13 at 16:52
  • @LightnessRacesinOrbit: I was using getline because I read on Cplusplus (the site) that usin cin with the '>>' operator can cause many errors on the input stream. I know ^^, Thanks for all the help. – fishycrakers Jan 09 '13 at 17:35