473,473 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Code Problem

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
Jul 22 '05 #1
15 6047
Dan M wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
Dan M wrote:
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
Jul 22 '05 #3
Dan M wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Karl Heinz Buchegger wrote:

Dan M wrote:

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.


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
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
Jul 22 '05 #6
<snip>

Ignore me - I've sussed it all

CHEERS EVERYONE

Dan
Jul 22 '05 #7

"Dan M" <da**@kent.ac.uk> wrote in message
news:cl**********@athena.ukc.ac.uk...
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
Jul 22 '05 #8
In message <cl**********@athena.ukc.ac.uk>, Dan M <da**@kent.ac.uk>
writes
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.

--
Richard Herring
Jul 22 '05 #9

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

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

Jul 22 '05 #10
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
Jul 22 '05 #11
In message <4R*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes

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.
--
Richard Herring
Jul 22 '05 #12
In message <wX*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
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)
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);
{
//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


--
Richard Herring
Jul 22 '05 #13

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
Jul 22 '05 #14

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:Em*******************@news.indigo.ie...

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!)
Just out of curiosity - in this specific example, where do you think would
the temporaries come from?

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).


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

Cheers
Chris
Jul 22 '05 #15
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
Jul 22 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
8
by: Andy B | last post by:
Before I do a no no on a newsgroup, I need to ask a question: What is the max number of lines of code you can/should post here before it gets too long?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.