Code Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan M

    #1

    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


  • Phlip

    #2
    Re: Code Problem

    Dan M wrote:
    [color=blue]
    > 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;[/color]

    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



    Comment

    • chris

      #3
      Re: Code Problem

      Dan M wrote:[color=blue]
      > 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
      >[/color]
      First of all, this is c++. Why use char* when you could use a std::string?[color=blue]
      > int replace (char *string)[/color]
      you need a { here[color=blue]
      >
      > int i= 0;
      >
      > int total= 0;
      >
      > while (*string)
      >
      > {
      >
      > if (*string+i)== ' '[/color]
      should be if ( *(string+i)==' ') I think
      [color=blue]
      > {
      >
      > total++;
      >
      > *(string+i) '-';[/color]
      do you mean *(string+i)=' '?[color=blue]
      >
      > }
      >
      > i++
      >
      > }
      >
      > return total;
      >[/color]
      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

      Comment

      • Karl Heinz Buchegger

        #4
        Re: Code Problem

        Dan M wrote:[color=blue]
        >
        > 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
        >[/color]

        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
        kbuchegg@gascad .at

        Comment

        • Karl Heinz Buchegger

          #5
          Re: Code Problem

          Karl Heinz Buchegger wrote:[color=blue]
          >
          > Dan M wrote:[color=green]
          > >
          > > 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
          > >[/color]
          >
          > 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.[/color]

          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
          kbuchegg@gascad .at

          Comment

          • Dan M

            #6
            Re: Code Problem

            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


            Comment

            • Dan M

              #7
              Re: Code Problem

              <snip>

              Ignore me - I've sussed it all

              CHEERS EVERYONE

              Dan


              Comment

              • Howard

                #8
                Re: Code Problem


                "Dan M" <dam5@kent.ac.u k> wrote in message
                news:cl8hs1$sds $1@athena.ukc.a c.uk...[color=blue]
                > 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;[/color]

                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


                Comment

                • Richard Herring

                  #9
                  Re: Code Problem

                  In message <cl8gud$hto$1@a thena.ukc.ac.uk >, Dan M <dam5@kent.ac.u k>
                  writes[color=blue]
                  >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[/color]

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

                  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

                  Comment

                  • JKop

                    #10
                    Re: Code Problem


                    Replacing spaces with hyphens?


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

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


                    -JKop

                    Comment

                    • JKop

                      #11
                      Re: Code Problem

                      JKop posted:
                      [color=blue]
                      >
                      > Replacing spaces with hyphens?
                      >
                      >
                      > void ReplaceSpacesWi thHyphens(char* str)
                      > {
                      > //Undefined Behaviour if supplied with
                      > //a null pointer
                      >
                      > for ( ; *str; ++str )
                      > {
                      > if ( *str == ' ' ) *str = '-';
                      > }
                      > }
                      >
                      >
                      > -JKop[/color]


                      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

                      Comment

                      • Richard Herring

                        #12
                        Re: Code Problem

                        In message <4RQdd.39725$Z1 4.14160@news.in digo.ie>, JKop <NULL@NULL.NULL >
                        writes[color=blue]
                        >
                        >Replacing spaces with hyphens?
                        >
                        >
                        >void ReplaceSpacesWi thHyphens(char* str)
                        >{
                        > //Undefined Behaviour if supplied with
                        > //a null pointer
                        >
                        > for ( ; *str; ++str )
                        > {
                        > if ( *str == ' ' ) *str = '-';
                        > }
                        >}
                        >[/color]
                        He wants to count them, too.


                        --
                        Richard Herring

                        Comment

                        • Richard Herring

                          #13
                          Re: Code Problem

                          In message <wXQdd.39726$Z1 4.14239@news.in digo.ie>, JKop <NULL@NULL.NULL >
                          writes[color=blue]
                          >JKop posted:
                          >[color=green]
                          >>
                          >> Replacing spaces with hyphens?
                          >>
                          >>
                          >> void ReplaceSpacesWi thHyphens(char* str)
                          >> {
                          >> //Undefined Behaviour if supplied with
                          >> //a null pointer
                          >>
                          >> for ( ; *str; ++str )
                          >> {
                          >> if ( *str == ' ' ) *str = '-';
                          >> }
                          >> }
                          >>
                          >>
                          >> -JKop[/color]
                          >
                          >
                          >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:[/color]
                          [color=blue]
                          >
                          >
                          >template<cla ss T, T before, T after>
                          >void ReplaceXwithY(T * str)[/color]

                          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(se q.begin(), seq.end(), before, after);
                          [color=blue]
                          >{
                          > //Undefined Behaviour if supplied with
                          > //a null pointer
                          >
                          > for ( ; *str; ++str )
                          > {
                          > if ( *str == before ) *str = after;
                          > }
                          >}
                          >
                          >or maybe:
                          >
                          >template<cla ss 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[/color]

                          --
                          Richard Herring

                          Comment

                          • JKop

                            #14
                            Re: Code Problem


                            [color=blue]
                            > template <class T>
                            > void Replace(T* str, T const & before, T const & after)[/color]

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

                            [color=blue]
                            > But you're generally better off using (and encouraging the use of)
                            > sequences:
                            >
                            > std::replace(se q.begin(), seq.end(), before, after);[/color]


                            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

                            Comment

                            • Chris Theis

                              #15
                              Re: Code Problem


                              "JKop" <NULL@NULL.NULL > schrieb im Newsbeitrag
                              news:EmTdd.3973 3$Z14.14270@new s.indigo.ie...[color=blue]
                              >
                              >[color=green]
                              > > template <class T>
                              > > void Replace(T* str, T const & before, T const & after)[/color]
                              >
                              > 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[/color]
                              machine[color=blue]
                              > code for both!)[/color]

                              Just out of curiosity - in this specific example, where do you think would
                              the temporaries come from?
                              [color=blue]
                              >
                              >[color=green]
                              > > But you're generally better off using (and encouraging the use of)
                              > > sequences:
                              > >
                              > > std::replace(se q.begin(), seq.end(), before, after);[/color]
                              >
                              >
                              > 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[/color]
                              boring,[color=blue]
                              > not as in I'm incapable of comprehending it).
                              >[/color]

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

                              Cheers
                              Chris


                              Comment

                              Working...