Code Problem

D

Dan M

Can anyone help me with this code?

This is the section and it comes up with a warning C4518: 'int' :
storage-class or type specifier(s) unexpected here; ignored on line 3

int replace (char *string)

int i= 0;

int total= 0;

while (*string)

{

if (*string+i)== ' '

{

total++;

*(string+i) '-';

}

i++

}

return total;



Any help gratefully appreciated!



Dan
 
P

Phlip

Dan said:
Can anyone help me with this code?

This is the section and it comes up with a warning C4518: 'int' :
storage-class or type specifier(s) unexpected here; ignored on line 3

int replace (char *string)

int i= 0;

You have no { above that 'int'.

When you code, always write the minimum that can compile and execute. Then
compile it, execute it, and predict the results. Don't write too much before
ensuring you can compile. All programmers work best like this.
 
C

chris

Dan said:
Can anyone help me with this code?

This is the section and it comes up with a warning C4518: 'int' :
storage-class or type specifier(s) unexpected here; ignored on line 3
First of all, this is c++. Why use char* when you could use a std::string?
int replace (char *string) you need a { here

int i= 0;

int total= 0;

while (*string)

{

if (*string+i)== ' '
should be if ( *(string+i)==' ') I think
{

total++;

*(string+i) '-'; do you mean *(string+i)=' '?

}

i++

}

return total;
you need a '}' here

Also note that it looks like you loop will just cycle around for ever.
Do you mean while(*(string+i)) in the while loop definition?
I'm not sure how/where you are learning c++, but I think you need to go
away and learn the basic syntax a little better..

Chris
 
K

Karl Heinz Buchegger

Dan said:
Can anyone help me with this code?

This is the section and it comes up with a warning C4518: 'int' :
storage-class or type specifier(s) unexpected here; ignored on line 3

OK. So start counting the lines:


1 int replace (char *string)
2
3 int i= 0;
4
5 int total= 0;
6
7 while (*string)
8
9 {
.....

Which one is line 3. Look it up. What else is the compiler
talking about? The keyword 'int' came unexpected. So look
up that line and where it says 'int'. Unexpected means the
compiler was completely baffled that the continuation of a line
had the keyword 'int' at exactly this position. But how can this
be? There is nothing to be continued, the 'int' is the starting
keyword for a definition (in your thoughts). Hmm. Look at the
line prior to the line flagged by the compiler. Look hard,
especially at the far right end. Notice something? There is
a ';' missing.
 
K

Karl Heinz Buchegger

Karl said:
OK. So start counting the lines:

1 int replace (char *string)
2
3 int i= 0;
4
5 int total= 0;
6
7 while (*string)
8
9 {
....

Which one is line 3. Look it up. What else is the compiler
talking about? The keyword 'int' came unexpected. So look
up that line and where it says 'int'. Unexpected means the
compiler was completely baffled that the continuation of a line
had the keyword 'int' at exactly this position. But how can this
be? There is nothing to be continued, the 'int' is the starting
keyword for a definition (in your thoughts). Hmm. Look at the
line prior to the line flagged by the compiler. Look hard,
especially at the far right end. Notice something? There is
a ';' missing.

Sorry: accidently hit 'send'

So when the compiler parses

int replace( char *string)

it looks for a continuation. Possible continuations are:
Either a ';' in case that the above is a function prototype
or a '{' in case that this is the start of a function definition.
In any case: 'int' cannot be there and that is what the compiler
is telling you.
 
D

Dan M

OK,

Thanks for the suggestions as to why it wasn't working

I am very new to C++ and am getting in over my head very quickly TBH

One last error in the code and its not apparant to me either, the compiler
errors don't seem to give any clues, or I don't know how to read them
properly yet

int replace (char *string);

{

int i= 0;

int total= 0;

while (*string)

i++

{

if (*(string+i)== ' ')

{

total++;

*(string+i)=='-';

}

}

return total;

}



The second line with the { is coming up with a missing function header (old
style formal list?) error, and I have no idea what that means. Last thing
before it compiles hopefully!



CHEERS

Dan
 
H

Howard

Dan M said:
OK,

Thanks for the suggestions as to why it wasn't working

I am very new to C++ and am getting in over my head very quickly TBH

One last error in the code and its not apparant to me either, the compiler
errors don't seem to give any clues, or I don't know how to read them
properly yet

int replace (char *string);

{

int i= 0;

Now you've added too much! If this is a function called "replace", then you
do NOT want the ';' there, just the '{'. Don't just add things without
thinking what they do. The ';' ends a statement in C++. But if this is a
function, you don't want to end the statement, you want to begin a compound
statement...the function body itself. And you do that by enclosing the
function body in '{' and '}' symbols. (Look at some code examples and read
your books, much more carefully.)

-Howard
 
R

Richard Herring

Dan M <[email protected]> said:
Can anyone help me with this code?

This is the section and it comes up with a warning C4518: 'int' :
storage-class or type specifier(s) unexpected here; ignored on line 3

Just the *one* ?
int replace (char *string)
// Assuming this is the function definition, not merely a declaration...
{ // this is the body of the function; it needs enclosing in { }
int i= 0;
int total= 0;
while (*string)
{
if
( // the condition clause of if needs to be enclosed in ()
(*string+i)== ' ' )
{
total++;
*(string+i)
= // you seem to have omitted an operator here
'-';
}
i++ ; // needs a terminator
}

return total;
} // end of function body
Any help gratefully appreciated!

That should make it compile.

I'll leave it as an exercise for the reader (a) to improve the style,
(b) to figure out why it won't do what you expect.
 
J

JKop

Replacing spaces with hyphens?


void ReplaceSpacesWithHyphens(char* str)
{
//Undefined Behaviour if supplied with
//a null pointer

for ( ; *str; ++str )
{
if ( *str == ' ' ) *str = '-';
}
}


-JKop
 
J

JKop

JKop posted:
Replacing spaces with hyphens?


void ReplaceSpacesWithHyphens(char* str)
{
//Undefined Behaviour if supplied with
//a null pointer

for ( ; *str; ++str )
{
if ( *str == ' ' ) *str = '-';
}
}


-JKop


Actually, may as well make it reusable:

(The following code is half-baked as I'm not sure if you're allowed to make
template paramaters refer to templated types...)

Well, if it does work, it will work with null terminated arrays of any type:


template<class T, T before, T after>
void ReplaceXwithY(T* str)
{
//Undefined Behaviour if supplied with
//a null pointer

for ( ; *str; ++str )
{
if ( *str == before ) *str = after;
}
}

or maybe:

template<class T>
template<T before, T after>
void Repla...


Yes... I could whip out my compiler and try it out... but I'm hungry and I'm
going to get some food now :-D...


-JKop
 
R

Richard Herring

JKop <[email protected]> said:
Replacing spaces with hyphens?


void ReplaceSpacesWithHyphens(char* str)
{
//Undefined Behaviour if supplied with
//a null pointer

for ( ; *str; ++str )
{
if ( *str == ' ' ) *str = '-';
}
}
He wants to count them, too.
 
R

Richard Herring

Replacing spaces with hyphens?


void ReplaceSpacesWithHyphens(char* str)
{
//Undefined Behaviour if supplied with
//a null pointer

for ( ; *str; ++str )
{
if ( *str == ' ' ) *str = '-';
}
}


-JKop


Actually, may as well make it reusable:

(The following code is half-baked as I'm not sure if you're allowed to make
template paramaters refer to templated types...)

Well, if it does work, it will work with null terminated arrays of any type:[/QUOTE]
template<class T, T before, T after>
void ReplaceXwithY(T* str)

Why not:

template <class T>
void Replace(T* str, T const & before, T const & after)

Now you can use template type deduction and get some degree of
type-checking.

But you're generally better off using (and encouraging the use of)
sequences:

std::replace(seq.begin(), seq.end(), before, after);
 
J

JKop

template <class T>
void Replace(T* str, T const & before, T const & after)

I tend to choose template parameters over function arguments wherever
possible, as everything will be done at compile time and you'll have no
temporaries.

(But yes, I do realize that a certain compiler may produce the same machine
code for both!)

But you're generally better off using (and encouraging the use of)
sequences:

std::replace(seq.begin(), seq.end(), before, after);


hmm... I really need to get me a good book on the Standard Library. I've
tried reading the STL part of TCPPL but I find it real cryptic, monotanous
and generally hard to read in places. (hard to read as in I find it boring,
not as in I'm incapable of comprehending it).


-JKop
 
C

Chris Theis

JKop said:
I tend to choose template parameters over function arguments wherever
possible, as everything will be done at compile time and you'll have no
temporaries.

(But yes, I do realize that a certain compiler may produce the same machine
code for both!)

Just out of curiosity - in this specific example, where do you think would
the temporaries come from?
hmm... I really need to get me a good book on the Standard Library. I've
tried reading the STL part of TCPPL but I find it real cryptic, monotanous
and generally hard to read in places. (hard to read as in I find it boring,
not as in I'm incapable of comprehending it).

In this case I'd recommend to get a copy of Nikolai Josuttis' "The C++
Standard Library".

Cheers
Chris
 
J

JKop

In this case I'd recommend to get a copy of Nikolai Josuttis' "The C++
Standard Library".

Cheers
Chris


Noted, thanks.

Will look for it next time I'm at the library!


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top