473,813 Members | 3,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}

Mar 2 '07 #1
8 2259
ja*******@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
Mar 2 '07 #2
<ja*******@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.
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*******@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?
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*******@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
Mar 3 '07 #9

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

Similar topics

3
2875
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 ... it is the most elegant and graceful language I have ever used (Fortran, Cobol, Basic, many assemblers, Forth, C, VB, etc.). I don't have the resources or the
3
2994
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
1615
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 numbers two decimal places long like 10.01 I only know how to define numbers as int or double. Do I use float? Also, I'm using Visual Studio .NET is there anyway to keep the compiler on the screen long enough to actually see what it's outputting. ...
8
2013
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 msg that im getting: "Error executing cl.exe" this is the code that I have, but I know whats causing it, ill just show you the whole thing first though //file: Quadratic
14
2296
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 this string "1,2,3,4,..." Into "...4,3,2,1" This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question
1
1920
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 that was generated has the following code at the end. <System.Xml.Serialization.SoapTypeAttribute("Map",
10
2432
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 of free IDE. So I discovered that I needed to download a virtual server, so I downloaded OmniSecure and followed the set up instructions as far as I could figure them out. So here is where I'm stuck. 1) While trying to set up and configure...
1
1806
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. I realise that I learn better if I explain things as I go along. My suggestion is, I start a tutorial as I learn the concepts of the language from the beginning though I'm not exactly a beginner, I've started c++ a couple of months ago and I...
10
2159
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 like python, ruby etc. I would like to know if there is a prerequisite to learning any computer language, is there something I have to learn before learning any computer language, like a basic or core?
22
18154
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 programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10417
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10139
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9220
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7678
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.