473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Beginner in C++ need help

calculate the power of 2 integers. For example, power(2,3)=8,
power(3,2)=9,power(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.program 2 use "do-while"statement to do the
calculation. program 3 use "for"statement 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;
}

Mar 2 '07 #1
8 2220
ja*******@hotmail.com wrote:
calculate the power of 2 integers. For example, power(2,3)=8,
power(3,2)=9,power(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.program 2 use "do-while"statement to do the
calculation. program 3 use "for"statement 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
Mar 2 '07 #2
<ja*******@hotmail.comwrote:
calculate the power of 2 integers. For example, power(2,3)=8,
power(3,2)=9,power(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.program 2 use "do-while"statement to do the
calculation. program 3 use "for"statement 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.
Mar 2 '07 #3
#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.

Mar 3 '07 #4
can anyone help?
Mar 3 '07 #5
* ja*******@hotmail.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?
Mar 3 '07 #6
>
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
Mar 3 '07 #7
You are asking it in wrong forum, just ask it in comp.lang.c forum

Mar 3 '07 #8
ja*******@hotmail.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
Mar 3 '07 #9

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

Similar topics

3
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python...
3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
7
by: BobJohnson | last post by:
Just started learning C++ and I need some help with my homework, shouldn't take long for people around here. I need to create a simple money calculator but I don't know how to make the output...
8
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error...
14
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn...
1
by: hl | last post by:
Hi, I'm a beginner and need a little help with getting data back from a web service. I am using VB.Net and have added a web reference to a Wsdl that was provided to me. My reference.vb file...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
1
by: Blue_hatter | last post by:
Hey Guys, I'm a newbie to the whole C++ Programming thing as I think I said before in a post. The thing is, I have this idea that might help me to learn at a better pace than I am doing currently....
10
by: hamza612 | last post by:
I want to start learning how to program. But I dont know where to start. From what I've heard so far c++ is not a good lang. to learn as a beginner because its very complicated compared to others...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.