how to outout 0

hi i am having trouble having 0 loop at the beginning if i type in 01234
it will just output -1-22-333-4444 but when i type in 1023 it will show 1-0-22-333 correctly. ive been trying to create a statement that would make 0 = 1 then output using cout <<"0"; i believe the problem is in if (b == count)

note: i am a beginner and

here is my program

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>

using namespace std;
int main ()
{
	int num; 
	char choice;
	
	do {
		int count = 0, remain, quotient;
		
		cout << "\nEnter an integer value: ";
		cin >> num;
		
		if (num <= 0 || num >= 2147483647) // maximum storage memory for int
			cout << "\nInvalid number!\n";
		else {
			for (int a = num; a != 0; ++count) { // counts number of digits 
				a /= 10;
			}
			for (int b = count; b > 0; b--) { // isolates each digit
				quotient = num / (int) pow(10, b - 1);
				remain = num % (int) pow(10, b - 1);
				if (b == count)
					cout << "";
				else 
					cout << "-";	
				if (quotient == 0)
				cout << "0";		
					
				for (int c = quotient; c > 0; c--) { // repeats digits according to their value
					cout << quotient;
					
					
					num = remain;
				}
			}
			cout << "\n\nEnter <y>es to continue or <n>o to discontinue: ";
			cin >> choice;
		}
	} while (choice == 'y' || choice == 0);
	cout << "\nProgram ended\n";
}
Last edited on
The number 012345 is stored as just an int. Its the same number as 12345 or 00000012345 (ignoring octal notation).
You would have to originally store the "number" as a string, to keep the leading zero info.

Quick example I made:
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
27
28
29
30
31
32
// Example program
#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::string num;
  std::cin >> num; // input the "number" as a string
  
  for (int i = 0; i < num.length(); i++)
  {
      // store character of the string into a
      // stringstream for later conversion
      std::stringstream ss;
      ss << num[i];
      
      // convert the character into an int:
      int n;
      if (ss >> n) // check success of conversion
      {
          // print the character out n times:
          for (int i = 0; i < n; i++)
          {
              std::cout << n;
          }
          std::cout << std::endl;
      }

  }
  
}


PS: Welcome to the forum=)

Also please read this for next time:
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
we are only allowed to use #cmath and #iostream but thank you anyways
You could input it as a string then use a for loop and switch statement like so
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
27
28
29
30
31
32
33
34
35
for(int i = 0; i < number.length(); i++)
{
     switch(number[i])
     {
     case '0':
          cout << '0';
          break;
     case '1':
          cout << '1';
          break;
     case '2':
          cout << "22";
          break;
     case '3':
          cout << "333";
          break;
     case '4':
          cout << "4444";
          break;
     case '5':
          cout << "55555";
          break;
     case '6':
          cout << "666666";
          break;
     case '7':
          cout << "7777777";
          break;
     case '8':
          cout << "88888888";
          break;
     case '9':
          cout << "999999999";
     }
}

That should solve the problem and satisfy the constraints.
closed account (E0p9LyTq)
You could input it as a string

The OP stated they can only use <cmath> and <iostream>, <string> is "off-limits."

One work-around could be a char array, use the null terminator for the loop comparison.
Yes my apologies about the input. But yes try using a char array then use array.size() for the condition in the for loop.
closed account (E0p9LyTq)
He can't use array.size(), that is a C++11 container construct he can't use. It would require he include the <array> header.

Create a C-string char array: char input[256];

Use std::cin.getline() to input the entire number as a C-string, including the null terminator '\0'

Then use the null terminator as the test condition in the for loop:
for(int i = 0; input[i] != '\0'; i++)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

int main()
{
   std::cout << "Enter a number: ";
   char number[256];
   std::cin.getline(number, 256);
   
   for (int i = 0; number[i] != '\0'; i++)
   {
      switch (number[i])
      {
      case '0':
         std::cout << "0 ";
         break;
      
      case '1':
         std::cout << "1 ";
         break;
      
      case '2':
         std::cout << "22 ";
         break;
      
      case '3':
         std::cout << "333 ";
         break;

      case '4':
         std::cout << "4444 ";
         break;

      case '5':
         std::cout << "55555 ";
         break;

      case '6':
         std::cout << "666666 ";
         break;

      case '7':
         std::cout << "7777777 ";
         break;

      case '8':
         std::cout << "88888888 ";
         break;

      case '9':
         std::cout << "999999999 ";
      }
   }
   std::cout << "\n";
}


Enter a number: 01258
0 1 22 55555 88888888

I used nothing more than some core C++ keywords and the <iostream> library. No need for <cmath> at all.
Last edited on
Topic archived. No new replies allowed.