-2

So i am doing this for school and i dont know whats the problem The task of program is to read the number inputed and then write it from back to start like 123 it will return it 321 the code i wrote seems all ok to me but only error its giving is Id returned 1 exit status. I have absolutely no idea what could be so wrong in such small code so here it is. to make it easier to understand Nc is digits number and s is multiplier.c is like a last digit i extract from n before i divide it by 10 in my while loop. n is inputed number n1 is wanted number.

    int main(int argc, char** argv) {
 int n,nc,n1,c,s;
 printf("Enter number: \n");
 scanf("%d",&n);
 while(n>0){
    n/=10;
    nc+=1;
 }
 s=pow(10,nc);
 while(n>0){
    c=n%10;
    n1+=c*s;
    s/=10;
    n/=10;

 }
printf("New number is %d",n1);

system("pause");
return 0;

}

Leko
  • 13
  • 4
  • 1
    Well, you didn't link something to the program, and probably it's -lm. – Innot Kauker Mar 04 '15 at 21:51
  • 2
    Is this the actual code you are running? if not, please post it here. Since you did not initialize nc, the number value that will be given to it is unpredictable, but that is just the first thing I see. There are multiple problems with this code. – FCo Mar 04 '15 at 21:52
  • this is the code im using – Leko Mar 04 '15 at 21:54
  • i have all the needed headers but i forgot to copy team i guess thats no problem – Leko Mar 04 '15 at 21:54
  • are you `#include` - ing anything? – FCo Mar 04 '15 at 21:55
  • of course i include iostream, stdio and math – Leko Mar 04 '15 at 21:56
  • First thing is that you can't include iostream in C. Please see: http://stackoverflow.com/questions/1844223/include-iostream-in-c . Please start with that. – FCo Mar 04 '15 at 21:59
  • this is c++ actually – Leko Mar 04 '15 at 22:00
  • actually something between c and c++ but everything was working with iostrieam untill now – Leko Mar 04 '15 at 22:00
  • What are you using as a compiler? – FCo Mar 04 '15 at 22:01
  • 2
    This isn't the answer to your question, but I would recommend to choose better names for variables. The ones you have (c, nc, n1...) mean nothing, in fact you had to explain their meaning. Instead, give them better names, like: n -> input, n1 -> output (or reverted_digits), Nc -> digits_number, and so on. It would make it a lot clearer for you too - imagine you have to modify this program one month from now, how long would it take you to understand how it works? If you used better names it would take much less. – Fabio says Reinstate Monica Mar 04 '15 at 22:02
  • thanks for your advice but im used with those names cuz in my language cifra=digit so i always use c isntead cifra then stepen=exponent so i know s is stepen in my bigger projects i use clear names but this is just a simple test-example – Leko Mar 04 '15 at 22:05

1 Answers1

0
int string2int(string input)
/// converts a string into an integer
{
    int output = 0;
    unsigned len = input.length();
    for (unsigned i = 0; i < len; i++)
    {
        char u = input[i]; // getting char from string
        int y = int(u); y -= '0'; // using character subtraction to make int
        output += y * pow(10, len - i - 1); // moving digit to appropriate spot
    } // the len - i - 1 is what makes the output go in correct order
    return output;
}

it should be noted that pow is in this context a function of my creation, that simply uses a loop, if you wish to use standard pow, you should round the result, it should also be noted that this function uses strings.

For the purposes of reading the user's input I would recommend using std::cin, which uses a similar syntax to std::cout and is really more convenient

user4578093
  • 231
  • 1
  • 3
  • 10