Beginner in C++ need help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jasson118@hotmail.com

    #1

    Beginner in C++ need help

    calculate the power of 2 integers. For example, power(2,3)=8,
    power(3,2)=9,po wer(4,0)=1. Not allow use power math function.Two
    integers are read from input by scanf. The program must produce an
    error message when the user enter a negative number, able to calculate
    the powers until terminate by user "by entering ctl-Z". output show
    both input integer and the result. program 1 use "while" statement to
    do the calculation.pro gram 2 use "do-while"statement to do the
    calculation. program 3 use "for"statem ent to do the calculation.

    The following is what I did for the "for" statement. It's not working.
    I also missed the part of showing an error message when the user enter
    a negative number

    #include "stdio.h"
    int main()
    {
    int a,b,loop,result ;
    if ((scanf("%d%d", &a,&b)==EOF) )
    {
    printf("\n Terminate by user\n");
    break;
    }
    else
    printf("Enter the base and power:\n");
    scanf("%d%d",&a ,&b);
    for (loop=1; loop<=b; loop++)
    {
    result=result*a ;
    }
    printf(" Base:",a);
    printf(" power:",b);
    printf(" result:",result );
    return 0;
    }

  • John Harrison

    #2
    Re: Beginner in C++ need help

    jasson118@hotma il.com wrote:
    calculate the power of 2 integers. For example, power(2,3)=8,
    power(3,2)=9,po wer(4,0)=1. Not allow use power math function.Two
    integers are read from input by scanf. The program must produce an
    error message when the user enter a negative number, able to calculate
    the powers until terminate by user "by entering ctl-Z". output show
    both input integer and the result. program 1 use "while" statement to
    do the calculation.pro gram 2 use "do-while"statement to do the
    calculation. program 3 use "for"statem ent to do the calculation.
    >
    The following is what I did for the "for" statement. It's not working.
    I also missed the part of showing an error message when the user enter
    a negative number
    >
    #include "stdio.h"
    int main()
    {
    int a,b,loop,result ;
    if ((scanf("%d%d", &a,&b)==EOF) )
    {
    printf("\n Terminate by user\n");
    break;
    }
    else
    printf("Enter the base and power:\n");
    scanf("%d%d",&a ,&b);
    for (loop=1; loop<=b; loop++)
    {
    result=result*a ;
    }
    printf(" Base:",a);
    printf(" power:",b);
    printf(" result:",result );
    return 0;
    }
    >
    It's not working for several reasons.

    1)

    scanf("%d %d",&a,&b);

    Note the space between the two %d

    2)

    for (loop=1; loop<=b; loop++)
    {
    result=result*a ;
    }

    This loop is fine, it's says multiply result by a and do it b times,
    which is exactly what you want. But ask yourself these questions. What
    is the value of result before you start this loop? What should it be
    before you start this loop?

    3)

    The main mistake though is something that isn't very clear from the
    program description. This program needs two loops, one inside the other.

    The outer loop is, loop until the user hits Ctrl+Z or enters a negative
    number. The inner loop is the one you've already written to calculate
    result.

    It's not clear from the instructions about use a for loop, use a while
    loop which loop is being talked about. I would guess the inner loop.

    So I would write something like this

    for (;;) // loop forever
    {
    get input
    if (Ctrl+Z or negative number)
    break; // stop looping forever
    do calculation
    print results
    }

    john

    Comment

    • osmium

      #3
      Re: Beginner in C++ need help

      <jasson118@hotm ail.comwrote:
      calculate the power of 2 integers. For example, power(2,3)=8,
      power(3,2)=9,po wer(4,0)=1. Not allow use power math function.Two
      integers are read from input by scanf. The program must produce an
      error message when the user enter a negative number, able to calculate
      the powers until terminate by user "by entering ctl-Z". output show
      both input integer and the result. program 1 use "while" statement to
      do the calculation.pro gram 2 use "do-while"statement to do the
      calculation. program 3 use "for"statem ent to do the calculation.
      >
      The following is what I did for the "for" statement. It's not working.
      I also missed the part of showing an error message when the user enter
      a negative number
      >
      #include "stdio.h"
      int main()
      {
      int a,b,loop,result ;
      if ((scanf("%d%d", &a,&b)==EOF) )
      {
      printf("\n Terminate by user\n");
      break;
      }
      else
      printf("Enter the base and power:\n");
      scanf("%d%d",&a ,&b);
      for (loop=1; loop<=b; loop++)
      {
      result=result*a ;
      }
      printf(" Base:",a);
      You misunderstand the requirements for the parameters given to printf.

      Try something like this:
      printf("Base %d \n", a);
      printf(" power:",b);
      printf(" result:",result );
      return 0;
      }
      I would isolate the computation in a function, then you can use the same
      "shell" for the three programs you have to write. The outer shell is an
      "infinite" loop looking for EOF or certain cases of bad input. If you
      isolate the computation it is simply cut and paste to come up with the
      repetitive part of the three programs.

      Your instructor allows you to ignore other forms of bad input: non-digit
      input for example, and numbers too large. In general n^m will not fit in an
      integer.


      Comment

      • jasson118@hotmail.com

        #4
        Re: Beginner in C++ need help

        #include "stdio.h"
        #include "assert.h"
        int main()
        {
        int a,b,loop,result ,ret;
        printf("Base:") ;
        ret=scanf("%d", &a);
        printf("power:" );
        scanf("%d",&b);
        if(ret==0)
        { //could not read from std input
        fprintf(stderr, "Invalid input\n");
        }
        else if(ret==EOF)
        { //read from file and EOF reached
        fprintf(stderr, "EOF reached\n");//or silentlty return
        return -1;
        }
        else
        { //read is success
        assert(ret==1);
        if(a<0)
        { // user entered negative value
        fprintf(stderr, "No negative value please\n");
        return -1;
        }
        }

        result=1;
        for (loop=1; loop<=b; loop++)
        {
        result=result*a ;
        }
        printf("Base: %d \n", a);
        printf("power:% d \n", b);
        printf("result: %d \n",result);
        return 0;
        }

        I made some changes to my code.
        The program works fine, but just unable to continue testing after I
        press any key, the window closes.

        Comment

        • jasson118@hotmail.com

          #5
          Re: Beginner in C++ need help

          can anyone help?


          Comment

          • Alf P. Steinbach

            #6
            Re: Beginner in C++ need help

            * jasson118@hotma il.com:
            #include "stdio.h"
            #include "assert.h"
            int main()
            {
            int a,b,loop,result ,ret;
            printf("Base:") ;
            ret=scanf("%d", &a);
            printf("power:" );
            scanf("%d",&b);
            if(ret==0)
            { //could not read from std input
            fprintf(stderr, "Invalid input\n");
            }
            else if(ret==EOF)
            { //read from file and EOF reached
            fprintf(stderr, "EOF reached\n");//or silentlty return
            return -1;
            }
            else
            { //read is success
            assert(ret==1);
            if(a<0)
            { // user entered negative value
            fprintf(stderr, "No negative value please\n");
            return -1;
            }
            }
            >
            result=1;
            for (loop=1; loop<=b; loop++)
            {
            result=result*a ;
            }
            printf("Base: %d \n", a);
            printf("power:% d \n", b);
            printf("result: %d \n",result);
            return 0;
            }
            >
            I made some changes to my code.
            The program works fine, but just unable to continue testing after I
            press any key, the window closes.
            Run the program from the command line (Google "command line").

            By the way, why don't you use the C++ i/o facilities?

            The program above is C, with nothing C++-specific.

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • John Harrison

              #7
              Re: Beginner in C++ need help

              >
              I made some changes to my code.
              The program works fine, but just unable to continue testing after I
              press any key, the window closes.
              >
              Well either you need to run the program again. Or if you don't want to
              do that it's like my earlier post said, you need a second outer loop in
              your program.

              john

              Comment

              • dervaish

                #8
                Re: Beginner in C++ need help

                You are asking it in wrong forum, just ask it in comp.lang.c forum

                Comment

                • Alan Johnson

                  #9
                  Re: Beginner in C++ need help

                  jasson118@hotma il.com wrote:
                  #include "stdio.h"
                  #include "assert.h"
                  int main()
                  {
                  int a,b,loop,result ,ret;
                  Everything from here ....
                  printf("Base:") ;
                  ret=scanf("%d", &a);
                  printf("power:" );
                  scanf("%d",&b);
                  if(ret==0)
                  { //could not read from std input
                  fprintf(stderr, "Invalid input\n");
                  }
                  else if(ret==EOF)
                  { //read from file and EOF reached
                  fprintf(stderr, "EOF reached\n");//or silentlty return
                  return -1;
                  }
                  else
                  { //read is success
                  assert(ret==1);
                  if(a<0)
                  { // user entered negative value
                  fprintf(stderr, "No negative value please\n");
                  return -1;
                  }
                  }
                  >
                  result=1;
                  for (loop=1; loop<=b; loop++)
                  {
                  result=result*a ;
                  }
                  printf("Base: %d \n", a);
                  printf("power:% d \n", b);
                  printf("result: %d \n",result);
                  down to here needs to be inside an "infinite" loop. You typically see
                  this done as John Harrison mentioned earlier in the thread:

                  for (;;)
                  {
                  // Body of the loop
                  }

                  The reason a for loop is used for that is because some compilers issue a
                  warning when the condition of a while loop is always true.
                  return 0;
                  }
                  >
                  I made some changes to my code.
                  The program works fine, but just unable to continue testing after I
                  press any key, the window closes.
                  >
                  On an unrelated note, this thread is a perfect example of the correct
                  way to ask for homework help. Usenet needs "sticky" posts. :-)

                  --
                  Alan Johnson

                  Comment

                  Working...