Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
a do-while calculator Posted by Newb1001 on 29 Mar 2011 at 11:33 AM
// Hello, I'm trying to develop a calculator because I think it would be the best program-type to learn and expanding programming skills. I am able to compile a workable console-calculator that reads the source once then exits. I don't want this, so,, I am trying to add a do-while loop to this application.

my-self developed theory (as-if all of these are supposed to work) is a do-while loop is like making an egg with two shells. lets call these shells the main-shell and the switch-shell because this is how I'm trying to apply this loop. My sources looks something like

int main()
{
cout<< "Press + , -. * or /"<<endl;
cout<< "Press Q to quit"<<endl;
cin>> (ch);

do if (ch!= 'q')

switch (ch)
{
case 'all possible !q's':
cout<<"enter numbers"<endl;
cin>>num_a>>num_b;
result= num_a () num_b;
cout<<result;
break;
}
while (ch=='q')
return 0;
}

If someone can expond on my understanding I will appreciate it and if i solve my problem I'll post Thanks in advance.

Newb1001
Report
Re: a do-while calculator Posted by pseudocoder on 29 Mar 2011 at 12:29 PM
I'm not quite sure what you're wanting to do in terms of the two egg shells, but maybe this will help.

...
#include <cmath>  // or math.h
#include <cctype> // or ctype.h
...

int main(void) {
   ...
   char choice;

   do {

      cout << "Enter operator (+, -, /, *): ";
      cin >> choice; while(cin.get() != '\n'){}

      switch(tolower(choice)) {
         case '+' : // do something
                    break;
         case '-' : // do something
                    break;
         case '/' : // do something
                    break;
         case '*' : // do something
                    break;
         case 'q' : cout << "the end"; 
                    break;
         default  : cout << choice << " is not a valid operator";
      }

   } while(tolower(choice)) != 'q');
   ...
}


HTH

Report
Re: a do-while calculator Posted by Newb1001 on 30 Mar 2011 at 1:32 PM
Thank you so much.
Report
Re: a do-while calculator Posted by pseudocoder on 1 Apr 2011 at 7:02 PM
no worries. :)
Report
Re: a do-while calculator (I need help editing my code) Posted by Newb1001 on 1 Jun 2011 at 4:15 PM
In the quest of expanding the calculating power of my calculator I added a switch and another do-while loop.
What I have added to my code, which basically looks like this

Count>> "Press E for Exponents and M for basic math"<<endl;
Cin>> (ch1);

Switch (ch != 'M')

do
{
Cout>> "enter the power."<<endl;
Case '1':
cout>> "enter the base."
Cin>> (a)
results=a
cout>> results <<endl;

case '2':
cout<< "enter the base."
Cin>> (a)
results = a*a
cout<< results <<endl;

...etc...}

As you can see the aforementioned code makes for a lot of scripting. What I need help with is understanding the for loop or figuring out a simple mathmatical algorithm that will work to process exponential equasions.

I am having difficulty understanding the for loop entirely.

as for the algorithm goes I have started working on one

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int a, b, c, d, i, r;

cout<<"Talk."<<endl;
cin>>a>>b;


c = b - 1;
d = a - 1;
i = d * d + d + d + 1;

if (b <=2);
do
{

r = i;

cout<< r ;
break;
}

while (b<=2);

if (b>2);
do {

r= b*i;

cout<< r;

break;
}
while (b>2);

return 0;
}

what this algorithm says is basically 2 square plus 2 and 3 = 3 squared
so if a = 3 and b= 2
then a-1 = d and d * d + d + d + 1= 3 squared

But when I try to equate a to the power of 4, i get incorrect values using

a(d*d+d+d+1)

because the aformentioned formula works with cubes and I understand that my variable b and c are not functioning in the code other than b working with the switch. I wrote c premptively and I need to figure out how to make b inneract with my formula in a way for my results to equate the desired power (if that makes sense).

also my results display the value of i then r instead of just r
like 4 cubed displays

1664

etc.

Any feedback on the algorithm or the for loop will be apprichiate.

thanks in advance,
Newb1001
Report
This post has been deleted. Posted by fuatgenc on 22 Jun 2011 at 3:11 PM
This post has been deleted.
Report
This post has been deleted. Posted by fuatgenc on 22 Jun 2011 at 3:13 PM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.