Suggestions for my project?

Hey all, I am fairly new with C++ and I registered here to ask for suggestions on how I can create my project.

I proposed to my professor that I will create a scientific calculator with "additional functions". By additional functions I mean like a SC with that can solve the missing side of a right triangle with the pythagorean theorem, an SC that can convert values to various english measurements, and other stuff that I can think about. Another thing that I added is that I will create a calculator that can convert any decimal value to hecadecimal, binary and octal.

But now I can't seem to think how can I start this. I have thought of using switches and functions to distinguish each operation to be used. I already stated in my proposal that my calculator can only do one operation at a time and my professor accepted that fact. So what can I do to exclude each and every function?

I have also thought about like saving the value computed and then the user can choose what to operation to use next on the newly acquired value.

Even though my professor approved my proposal he exclaimed that my calculator should include graphics and sound effects, how can I do that? The only thing that I thought for graphics is to cout a layout of some sort. And about the sound thing the beep is the only one I can think of.

So far we have only studied up to functions (but I think we will be only learning up to arrays by the end of this term). What we have tackled so far is:

basic cin and cout
if and else statements
switches
loops
functions

That's all of it. All very basic.

Well, that's all. Hope I didn't bore you or anything. Thank you so much for reading and any help and suggestions on how I can create this program will surely be appreciated. Thank you and good day!
Hey all, I was trying to make a decimal to binary converter for this calculator project of mine without using the math library and I came up with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main(void)
{
    int num, ctr1, ctr2, divide, bin[100];
    
    cout<<"Enter Number: ";
    cin>>num;
    
    cout<<"\nThe binary equivalent is: \n";
    
    for (ctr1=0;num=0;ctr1++)
    {
        divide = num%2;
        bin[ctr1] = divide;
        num = (num/2);
    }
    
    for (ctr2=0;num>=0;ctr2--)
    {
        cout<<bin[ctr2];
    }
    
    system("pause");
} 


Only problem of mine now is, it displays an indefinite loop! I don't know where I went wrong though. Can someone please point out to me what I have done wrong? I believe it is during the array storage part.

I am using devc++ since visual c++ express is so confusing and visual studio is well, um expensive. Thanks!
Your first for loop, you are using 'num = 0' instead of 'num == 0'. (That loop will end after the first loop because of that).

The second loop your are testing whether num is >= 0, which is never changed so if it is true, the the loop will never end.
I tried substituting for (ctr1=0;num=0;ctr1++) to for (ctr1=0;num==0;ctr1++) and for (ctr1=0;num!=0;ctr1++)

but it still won't work. How come it still becomes an indefinite loop?
Your second for loop doesn't modify num at all, so at best it runs once, at worst it is an infinite loop.
I see, so I edited my code and produced this:

1
2
3
4
for (ctr2=ctr1;ctr2>=0;ctr2--)
    {
        cout<<bin[ctr2];
    }


I managed not to make it a indefinite loop. But the answer yields to a wrong one. I can't think of how I can display the output using the for loop.

Is it correct to initialize ctr2 as 0?

I don't know what should I put on the second loop to display the proper output.
Why don't you combine the two loops into one?

All your first loop does is parse out the bits, least to most significant, and store them in an array.
All your second loop does is output the contents of the array backwards.

You can get the Nth bit of an integer with

( integer >> N ) & 1; // yields the integer value 0 or 1



convert the loop into one? I am sorry but I can't understand the algo you just posted. Can you please explain it to me more?
You want to output an integer in binary. The first bit you want to output is the most signifcant bit, or bit 31 (assuming zero based and 32 bit integers).

So use the above formula to obtain the 31st bit, output it.
Then repeat for bit 30, 29, 28, and so on down to and including 0.

(Hint: that should give you an idea that looping on the bit number, 31 down to 0, will be helpful).

@tictock your algo is correct but there are two problems with your program
1.The first for loop where your are checking for "num==0".
you need to correct this condition, see how the for loop basically works
2.The second for loop where you are trying to access a garbage value from the array "bin[]".
here you are trying to access an element of array where you have not stored any value.
The value of ctr1 after the 1st for loop is not the length of array bin.


I am assuming you are doing something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(void)
{
    int num, ctr1, ctr2, divide, bin[100];
    
    cout<<"Enter Number: ";
    cin>>num;
    
    cout<<"\nThe binary equivalent is: \n";
    
    for (ctr1=0;num==0;ctr1++)
    {
        divide = num%2;
        bin[ctr1] = divide;
        num = (num/2);
    }
    for (ctr2=ctr1;ctr2>=0;ctr2--)
    {
        cout<<bin[ctr2];
    }
    system("pause");
} 
Last edited on
I re did the code and came up with the second and first loop as

1st loop: ctr1=0;num!=0;ctr1++

2nd loop: ctr2=ctr1-1;ctr2>=0;ctr2--
Thanks for the hints guys!

I plan on adding a converter thing on my calculator, and so far the only thing I have done is a meters to yard converter haha. I initially planned on making a converter for the most common lenght conversions (cm -m, feet - cm, etc.) but I can't think of any other way to do this without typing in too much code!

What I have thought is that the user should enter comething like cin>>digit>>unit>>unittobeconverted

but with something like that I'd have to make functions or cases for each and every conversion like one for cm-m, cm-ft, etc. Is there any other way to do this? I want to optimize my code but I don't know how!

Thanks to those who'll be suggesting stuff.
So the solution I was trying to lead you toward is:

1
2
for( int bit = 31; bit >= 0; --bit )
    std::cout << ( (  integer >> bit ) & 1 );


For your next step, since every unit-to-unit conversion is simply multiplying one unit by some constant to yield the value, consider making an array:

1
2
3
4
5
6
7
8
9
10
11
struct Conversion {
    const char* from;
    const char* to;
    double      conv_factor;
};

Conversion conversions[] = {
    { "cm", "in", 2.54 },
    { "in", "ft", 12.0 },
    /* etc. */
};


Now use a for loop to make a menu for the user. Have them enter a number and use
that number to index into the array to get the conversion factor. Then they enter
the "from" number, and you multiply by the conv_factor to get the result.
Last edited on
I'm sorry jsmith but I wasn't really able to understand your solution for my binary converter, even if I tried re reading it more than twice.

anyways, so what you are saying for the conversion part is that I should store the conversions in an array? I don't get this part right here:
1
2
3
4
5
Conversion conversions[] = {
    { "cm", "in", 2.54 },
    { "in", "ft", 12.0 },
    /* etc. */
};
Can you please help me, and explain it a much more, erm begginer manner? I am sorry if I am so dumb at this, I just really can't understand the code here.
So I'm suggesting that you want to display a menu, perhaps like this:


0. Convert "cm" to "in"
1. Convert "in" to "ft"

Enter the number of the conversion you'd like:


To make the above menu, you could write this loop:

1
2
3
for( int i = 0; i < 2; i++ )
   cout << i << ".  Convert " << conversions[i].from << " to " 
      << conversion[i].to << endl;


When the user enters, for example 1, you know they want to
convert "in" to "ft". Ask them to enter the number of "in".
Again, you get the word "in" by looking at conversions[choice].from,
where choice is the variable holding the number of the option the
user chose.

After they type in the number of inches, say 18, you take 18,
multiply it by conversions[choice].conv_factor, and output the
result. The units of the result are conversions[choice].to.





I see! So how should I store the arrays then? We have only tackled one dimensional arrays and I believe this is a two dimensional one right? Or is this strings? We have only tackled up to arrays now.

so variable declarations will something like:

int conversionFrom[ctr], conversionTo[ctr], conversionConst[ctr], num, chc, ctr

asking for the input would be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (ctr = 1; ctr>n ; ctr++)
{
cout<<ctr<<"Convert from"<<conversionFrom[ctr]<<"to"<<conversionTo[ctr]<<endl;
}

cout<<"Enter choice: ";
cin>>chc;
cout<<endl<<endl;

switch(chc):
{
case 1: 
cout<<"Enter value: ";
cin>>num;
conversionResult = num * conversionConst[chc];
cout<<endl<<"Converted value is: "<<conversionResult<<endl;
.
.
.
case n:
}


is it something like this? I'm sorry if this is incorrect but I am truly trying my best.
Last edited on
Yes - you have it right.

My example above is a one dimensional array. Each element of the array is a struct (have you covered structs?).

Just one thing -- your loop end condition on line 1 isn't right.
Yey! Thanks jsmith! But what is wrong with the first loop? I wanted it to start displaying from 1 and not from 0 that's why I made it that way. Or by making it 1 I did something wrong?
for (ctr = 1; ctr>n ; ctr++)

The for loop runs until the middle condition becomes false.

In order for the condition to be true, when ctr = 1, n has to be <= 1.

Perhaps you meant ctr < n? Or ctr <= n?
Topic archived. No new replies allowed.