newbie trying to use C here, so please don't treat me too harshly.
I want to try and make a program that can count and display the number of characters used in a sentence, but the program keeps making count_chars into an integer. How do I make the program count?
Here's my attempt at it.
#include <iostream>
#include <stdio.h>
void count_chars(char sentence[]);
int main()
{
int sentence;
std::cout << "Type a sentence and we will count the number of characters." << std::endl;
std::cin >> sentence;
std::cout << "Here is a list of letters you used in your sentence" << std::endl;
count_chars(sentence);
return 0;
}
It shows the errors:
- Error (active) E0167 argument of type "int" is incompatible with parameter of type "char *" Count_Chars 13
- Error C2664 'void count_chars(char [])': cannot convert argument 1 from 'int' to 'char []' Count_Chars
No idea what these mean
Any suggestions on improving it? I also want to try making it look like a graph, so any suggestions would be helpful.
Thanks.
CodePudding user response:
void count_chars(char sentence[]);
This tells the compiler "somewhere else, there is a function called count_chars
. It returns nothing at all (void
). It takes, as an argument a char
array.
First of all: you probably want a function called count_chars
to return something.
Then: there's no function here. Just the information that there might be a function with that signature somewhere else. I hope you wrote that function?
but the program keeps making count_chars into an integer.
No, that's not what's happening. I can only guess here, but you're probably misreading a compiler warning? Don't know.
#include <stdio.h>
No. No matter where you've learned this from: If someone tells you they're teaching you C and then #include <stdio.h>
appears, you need to find a different resource. This is C, not C ! Also, you're not using anything from that header!
int sentence; std::cin >> sentence;
Um, since when is a sentence an integer number? That makes no sense!
You seem to be in the "I'm copying code and are very overwhelmed by many things" phase of being a beginner. The sooner you leave that, the better! Get a C tutorial, a good one. Ideally, a good textbook. Learn step by step.
We have a list here exactly for people like you.
CodePudding user response:
I understand your goal, but I am a bit confused about what exactly is going on in your provided code.
You say "the program keeps making count_chars into an integer"
Your first line in main is "int sentence;" so that is what makes 'sentence' into an int type. Not sure if that's how you wrote it, or if you're saying that's how your IDE is repeatedly auto-correcting your declaration of 'sentence'. So that's a problem.
Secondly, you will run into issues when using std::cin to obtain multiple words (separated by spaces) from the console. I am still learning in this area, but std::cin needs modification to accept multiple words.
Lastly, you need to finish working on your count_chars function and use the correct data types, strings are a bit tricky.