Hello everyone. I am currently writing a program for class and I was having some trouble. Please note I am not on here for someone to do my homework I am here to find out what I am doing wrong so I can fix it in the future.
I am trying to write a program that will count the number of words a user enters and return that value.
1>c:\users\altizer\documents\visual studio 2010\projects\wk 2 p2\wk 2 p2\week 2 prog 3.cpp(37): error C2562: 'numbered' : 'void' function returning a value
I know the problems are 1. in the second function when I return times and 2. in the main function where it says "numbered(userstring, times);"
/* Andrew
Write a function that accepts a pointer to a C-string as an argument and returns the number of words in the string.
*/
#include <iostream>
#include <cstring>
usingnamespace std;
void numbered(char *);
int main()
{
char userstring[81];//int the c-string
cout << "Enter a word or short sentence fewer than 80 characters: ";
cout << "\n\n";
cin.getline(userstring, 81);
cout <<"\nYou typed: "<< userstring<<endl;
cout << "\n\nThis is what you typed backwards: "<<endl;
numbered(userstring, times);
cout << endl << endl;
return 0;
}
void numbered(char *strPtr, char ch)
{
int times = 0;
{
if (*strPtr == ch) // If the current character equals ch...
times++; // ... increment the counter
strPtr++; // Go to the next char in the string.
}
return times;
}
In your call to numbered in main, you're passing in times which has not been declared in that scope. From what your program is supposed to do, it seems you should just pass in ' ', as you want to get the number of words (note that there will likely by one more word than there are spaces, so watch out for that).
As for numbered...
1 2 3 4 5 6 7 8 9 10 11
void numbered(char *strPtr, char ch) // this needs to return an int, not be void
{
int times = 0;
{ // You need a loop here. Something like while (strPtr != null)
if (*strPtr == ch)
times++; //
strPtr++;
} // ends loop
return times;
}
Lastly, your declaration of numbered before main doesn't have the same parameters as the function itself.