Hello I am trying to write a program that removes spaces before and after and reduce to one between words and capitalize the first letter of each world and the other letters lowercase but I'm getting errors which I can't make heads and tail of.
|12|error: expected primary-expression before ')' token|
22|error: invalid conversion from `char' to `char*'|
|27|error: invalid conversion from `int' to `char*'|
|35|error: invalid operands of types `char*' and `char*' to binary `operator+'|

# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
void formatering ( char *str, char *tempstr, char *string);
int main()
{
char tempstr[80];
char str[80];
cin >> str;
gets (str);
formatering (str, tempstr, string);
return 0;
}
void formatering ( char *str, char *tempstr, char *string) //unsigned
{
    int a = 0;
    int b = 0;
while (*str != '\0')
    {
if  ( *str == ' ' && a == 1 && b == 1)
        { tempstr = *str;
        b = 0;
    }
    else if (a == 0 &&  (*str >= 'A' && *str <='Z') || (*str >= 'a' && *str <='z'))
    {
        tempstr  = toupper(*str);
        a = 1;
        b = 1;}
        else if (a == 1 &&  (*str >= 'A' && *str <='Z') || (*str >= 'a' && *str <='z'))
{
*str = tolower(*str + 1);
}
str++;
string = (string + tempstr);
}
tempstr--;
if (*str = ' ') {
   *tempstr--;}
   *tempstr++;
   *tempstr = '\0';
cout << string;
}

Recommended Answers

All 2 Replies

First, there is going to be an issue with the fact that you use the word "string" as a variable.... yeah no. You included iostream, and are using the std namespace, so string is a type. Can you have a variable named int? So, create a new char array, like bstring or something, and use that instead of "string". After you do that, you are going to have a bunch of issues with having to dereference your char arrays (such as string = (string + tempstr) ).

A suggestion here... stick with a programming style. You have a bunch of if's with opening braces { and then close them in some arbitrary and seemingly random location. Look at the first if you have (in the while loop inside of formatering). You have the if and expression on one line, then the brace on another, with code right there next to it. If you want to go ANSI style fine, but at least do that part right. then your last if, which is near the bottom there, has the brace on the same line as the if and the expression.... how 'bout you pick one and be consistent? Anyway, it will work once you change string, and make modifications to the unreferenced pointers so that they point to the values and not the locations. You will get a warning in g++ however, alerting you against the unsafe use of gets.

Why the heck you are using cstrings. They are old, they are evil and they can lead you in trouble.
Use std::string and be happy in life.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.