开发者

What is wrong with my program?? Basic C++

开发者 https://www.devze.com 2023-04-05 23:10 出处:网络
My assignment says: ---Rewrite the program by grouping the calculations into functions. In particular, you shouldintroduce at least the following functions:

My assignment says:

---Rewrite the program by grouping the calculations into functions. In particular, you should introduce at least the following functions:

---A function named toLowerCase that takes a single character as an input parameter and returns a character. The returned value should be identical to the input unless the input is an upper-case letter, in which case the returned value should be the lower-case equivalent to that letter.

---Another function named toLowerCase, this one taking a string as an input parameter and returning a string. The returned string should be identical to the input except that all upper-case letters have been converted to lowercase.

----A function named readText that takes a string as an output parameter (no return value) and that reads multiple lines of input from cin until either hitting end-of-input or encountering an empty line. (Note: readText should not write anything to cout.)

----A function named countCharacter that takes two parameters as input and return an integer. The first input parameter will be a string and the second a character. The returned value should be the number of time that character occurs in the string (zero if the character occurs nowhere in the string). This function should work as described for all legal characters (i.e., even though this program will only use it to count lower-case letters, it should work for lower-case letters, upper-case letters, punctuation, etc.) As you introduce each function, replace the code in main() by calls to your new functions as appropriate.

I keep getting error: expected primary-expression before char

#include<iomanip>
#include<iostream>
#include<string>

using namespace std;

 void readText(string& text);
 void toLowercase(string& text);
 void countcharacter(string& text, char []);
 char ToLowerCase();


int main (int argc, char** argv)
{

  string userinput; //Initialize string name userinput

  cout << "Enter text to be analyzed, ending with an empty line or end-of-input:" <<     endl;

 readText(userinput); //Read text from user

cout << "You've entered: \n" << userinput << '\n'; // Read input, line by line, until end of input or an empty line

toLowercase(userinput); //Output user input and if upper-case, convert to lower-case

 cout << "Lower case version of what you said: \n" << userinput << '\n';

 countcharacter(userinput); //Count characters i开发者_C百科n userinput

 ToLowerCase();

  return 0;
}


void readText(string& text)
{

 string line;
 getline (cin, line);


while (cin && line != "")
{
  text = text + line + "\n";
  getline (cin, line);
}

 /*for(std::string line; getline(std::cin, line) && !'\n' ; )

    input += line + '\n'; */
}

void toLowercase(string& text)
{

  const int ucLcOffset = 'a' - 'A';
  for (int i = 0; i < text.size(); ++i)
{
  char c = text[i];
  if (c >= 'A' && c <= 'Z')
text[i] = text[i] + ucLcOffset;
}

}

void countcharacter(string& text, char c)
{
// Count and report on each alphabetic character
  int totalCount = 0;

  for (c = 'a'; c <= 'z'; ++c)
    {
  // Count how many times c occurs in the text
      int charCount = 0;
          for (int i = 0; i < text.size(); ++i)
       {
         if (text[i] == c)
         ++charCount;
       }
  // report on character c
      cout << c << ":" << charCount << " " << flush;
      if ((c - 'a') % 10 == 9)
    cout << "\n";
      totalCount = totalCount + charCount;
    }
 // How many characters are left over?
     cout << "\nother:" << text.size() - totalCount << endl;



}

char ToLowerCase()
{
    
 char c = 'A';
 char Conversion;
 const int ucLcOffset = 'a' - 'A';

     if (c >= 'A' && c <= 'Z')
    Conversion = c + ucLcOffset;

    cout << "Your conversion for character A is: " << Conversion << '\n';

} 

Line 28 error. 'countcharacter(userinput); //Count characters in userinput'


The line:

#inlcude<iostream>

Should be:

#include <iostream>

Syntax highlighting is your friend.

Also... countcharacter() takes two arguments:

void countcharacter(string& text, char []);

But you only provide one:

countcharacter(userinput);


#inlcude<iostream>

You spelt include wrong.

 countcharacter(userinput); //Count characters in userinput

countcharacter() takes two parameters, not one.

It seems you want to count the occurrences of each lower case character ('a'-'z'). In this case you don't need to pass a second argument to countcharacter(). Change:

void countcharacter(string& text, char []);

to

void countcharacter(string& text);

and

void countcharacter(string& text, char []);

to

void countcharacter(string& text);

You will also have to declare char c in countcharacter().

It also seems you should change char ToLowerCase() to void ToLowerCase() as you don't seem to be returning anything.


I'm not a C++ guru, but the line

void countcharacter(string& text, char []);

Appears to be missing a parameter name.


Post the exact code you fed to your compiler. You wouldn't get "error: expected primary-expression before char" for what you posted, you'd get "error: invalid preprocessing directive #inlcude".

Copy-and-paste it, don't re-type it.

EDIT: fixed errror message

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号