Need help with programming assignment

.
Last edited on
Simplier method which scales to any number of digits and does not need right-to-left passes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>


int main()
{
    char digits[5] = { 0 };
    std::cout << "enter 4 digits number: ";
    std::cin >> digits;
    
    unsigned base;
    std::cout << "enter base: ";
    std::cin >> base;
    
    unsigned number = 0;
    for(unsigned i = 0; i < 4; ++i) {
        number *= base;
        number += digits[i] - '0';
    }
    std::cout << "number " << digits << " in base " << base << " is: ";
    std::cout << number;
}
enter 4 digits number: 1101
enter base: 2
number 1101 in base 2 is: 13
Last edited on
Thank you. Although we haven't learned a lot of the things you wrote in the program yet =[ . This is my issue so far with looking for an answer on google. A lot of the solutions consist of programming terms we still haven't gotten to in class. Is there any other way of writing this program with an even simpler type of programming or is this pretty much the simplest it gets? Again thank you.
What the problem you ahve with my code? Did you not learned loops yet? If so, you can take actual calcilations from your code replacing d4 with digits[3] and so on:
1
2
3
4
5
6
unsigned total = 0;
total += d[3] * 1;  
total += d[2] * base;
total += d[1] * base * base;
total += d[0] * base * base * base;
//Or you can leave your total1... final method, if you wish 


No we have not gotten to loops as of yet. We're still at a very basic level of programming, it's the third week of the class. thank you for helping
.
Last edited on
Everything was in my prevous code (I have made a mistake: forgot to normalize numbers):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Declare an array of char (c-string) containing 5 symbols (4 significant and trailing 0)
char digits[5] = { 0 }; 
std::cout << "enter 4 digits number: ";
//Read 4 digits into c-string (Overflow will happen if somebody enters more than 4)
std::cin >> digits;

//Conversion as said in your assigment:
//The program must first convert each bit from its ASCII value (such as 48 for 0 or 49 for 1, etc.) to its numeric value, such as 0, 1, etc
//value - '0' ← that is conversion
//It must then multiply each digit by its corresponding weight. 
//The least significant bit (rightmost bit) has a weight of 1 and as you go from right to left - 
//least to most significant bit - the weight of each digit is multiplied by the base 
//*1; *base; *base*base : multiply by weight
//Rightmost digit contained in d[3]; leftmost in d[0]
unsigned total = 0;
total += (d[3] - '0') * 1;  
total += (d[2] - '0') * base;
total += (d[1] - '0') * base * base;
total += (d[0] - '0') * base * base * base;

Some notes: do not use doubles to store integers.
You can make it simplier only with use of loops or standard library functions.
.
Last edited on
why can't you write it like this:
char digits[5] = 0 ;
It will give you a compile error: you are trying to assign variable digits (not individual element of it) value of 0 which is incompatible with type of char[5].

Braces here is a form of array initialization. More correctly was to write it as {'\0', '\0', \0', \0', \0'} but we used some standard guarantees. Actually you don't need to initialize it at all given that we will rewrite in just in a moment, but it is better be safe.


So basically '0' gives the ASCII value of zero which is 48? And since each digit that was input is already in ASCII it will equate to its int value once you subtract them by 48? And then give the base value after you multiply?
Yes
Last edited on
one more question, I'm so sorry bro lol.

so basically '0' only shows as a ASCII value when it's declared to a variable?

for example:
1
2
3
4
5
6

int total;

total = '0';

cout << total;



you cannot output an ASCII like this?
cout << '0';
char and int can be impicitly converted to each other. Basicly it means that you don't have to write stuff like: int x = static_cast<int>('0'); When we deduct char from char, they got converted to ints containing their ASCII values and then deduct one from other.
So to show ASCII value you need to convert char to int either implicitly assigning it to integer variable or by explicit cast. Try:
1
2
3
int x;
std::cout << (x = '0') << std::endl;
std::cout << static_cast<int>('1');
Topic archived. No new replies allowed.