i want to make a program that used to enter 7-digit number and determine the largest , smallest and the median and average of the seven number

and as for know i tink i got it but there something wrong again in my program can you chek it out and revise it ty

TNKS AGAIN

ERROR.CPP

Recommended Answers

All 5 Replies

Please post the code between code tags, and indent it properly.. it's horrible to look at code that has no indentation:

#include <stdio.h>
#include <conio.h>

int main()
{
  char charPtr[7];
  int smallest, largest, middle, intNo;
  int dividend=0; int modolus=0;
  int valid;

  do {
    printf("Enter a seven digit number:\n");
    scanf("%s", charPtr);

    valid=0; // counter for no of digits
    for (int i=0;i<sizeof(charPtr);i++)
    {
      intNo=(int)charPtr[i]-48; // trap if 0-9
      if (intNo>=0 && intNo<10) // trap invalid other than 0 to 9
        valid++; // increment counter
    }
    printf("valid=%d\n",valid);

  } while (valid!=7); // repeat if counter is not equal to 7

  for (int i=0;i<sizeof(charPtr);i++)
  {
    intNo=(int)charPtr[i]-48;

    if (intNo>=0 || intNo<10) // trap invalid other than 0 to 9
    {
      printf("%d = decimal=%d, ",i,intNo);
      if (i==0)
      {
        smallest=intNo;
        largest=smallest;
      }
      else
      {
        if (i==3)
          middle=intNo;
        if (intNo<smallest)
          smallest=intNo;
        if (intNo>largest)
          largest=intNo;
      }

      // compute binary
      printf("binary=");
      do {
        modolus=intNo % 2;
        dividend=intNo/2;
        intNo=dividend;
        printf("%d",modolus);
      } while(dividend!=0);
      printf("\n");
    }
    else
      printf("None Entered\n");
  }

  printf("Smallest=%d\n",smallest);
  printf("Largest=%d\n",largest);
  printf("Middle=%d\n",middle);
  printf("Decimal\t\t");

  float intDec=0; // initialize
  for (int i=sizeof(charPtr);i>0;i++)
  {
  }
}

The logic is alright, but there are many mistakes and improvements to be made:

1. If you want to store 7 digits as a char array, using the %s input, you need to have at least 8 chars in the array, because there is a null-character at the end of the string to tell where it ends. To be robust to the possibility that someone may enter more than 7 digits, you should also make the char array much bigger (like 256 or something).

2. You use the "function" sizeof() to get the length of the string in charPtr. This is wrong, the sizeof operator only gives the size of the type it is given, in this case the type is a pointer, so the result will be 4 bytes (32bit system) or possible 8 bytes (64bit). To get the length of the string pointed to by a char pointer, you need to use strlen() instead.

3. If this is supposed to be C++, you should know that printf and scanf are deprecated, and now std::cout and std::cin are in use. Also, std::string is much better than char* or char[] for holding strings. But for the rest of this, I will assume you are actually doing a C program and not a C++ program.

4. If you input a 7-digit number, why do you need to input it as a string? Why not use an integer number instead, using:

int InputNumber = 0;
scanf("%d",&InputNumber); //%d tells it to get a decimal number (integer) and store it in InputNumber.

This will be much easier to treat in your program than those char to int cast and minus 48 non-sense. To test if it is 7 digit, test if the number is greater than or equal to 10 million. Mod and divide by ten to get each digit out.

5. Look at the format specifiers to know how to output the value in all the required forms (octal and hexadecimal) without having to do a loop like you did for the binary value. There is no format specifier for binary though.

That's all for now.

I HAVE 1 ERROR ITS SAID MULTIPLE DECLERATION for i

HELP PLSS

use another letter.

ty mike now its work ^_^ tnkssssssssss

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.