switch statement problem
Oct 16, 2016 at 6:19pm UTC
So, I needed to make a program where you enlarge the digits' 0-9. For some reason, I can't seem to output the larger version of the digit. The program works, it's just that the switch statements aren't compiling.
Here's the code:
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#include <iostream>
using namespace std;
void banner();
void choice();
void digit0();
void digit1();
void digit2();
void digit3();
void digit4();
void digit5();
void digit6();
void digit7();
void digit8();
void digit9();
int main()
{
int digit = 9;
banner();
choice();
switch (digit)
{
case 0:
digit0();
break ;
case 1:
digit1();
break ;
case 2:
digit2();
break ;
case 3:
digit3();
break ;
case4:
digit4();
break ;
case 5:
digit5();
break ;
case 6:
digit6();
break ;
case 7:
digit7();
break ;
case 8:
digit8();
break ;
case 9:
digit9();
break ;
default :
cout << "I haven't figured that out yet" << endl;
break ;
}
return 0;
}
void banner()
{
cout << "Number Magnifier!" << endl;
cout << "See the number up close, and personal!" << endl;
cout << " ******************************************************************** " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * WELCOME! * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " ******************************************************************** " << endl;
}
void choice()
{
int digit;
cout << "Enter a digit (0-9) or press X to exit: " << endl;
cin >> digit;
cin.ignore(256, 'endl' );
}
void digit0()
{
cout << " 000 " << endl;
cout << " 0 0 " << endl;
cout << " 0 0 " << endl; //
cout << " 0 0 " << endl;
cout << " 0 0 " << endl;
cout << " 0 0 " << endl;
cout << " 000 " << endl;
}
void digit1()
{
cout << " 1 " << endl;
cout << " 1 " << endl;
cout << " 1 " << endl;
cout << " 1 " << endl;
cout << " 1 " << endl;
cout << " 1 " << endl;
cout << " 1 " << endl;
cout << " 1 " << endl;
}
void digit2()
{
cout << " 222 " << endl;
cout << " 2 2 " << endl;
cout << " 2 " << endl;
cout << " 2 " << endl;
cout << " 2 " << endl;
cout << " 2 " << endl;
cout << " 22222 " << endl;
}
void digit3()
{
cout << " 333 " << endl;
cout << "3 3" << endl;
cout << " 3 " << endl;
cout << " 33 " << endl;
cout << " 3 " << endl;
cout << "3 3" << endl;
cout << " 333 " << endl;
}
void digit4()
{
cout << " 4 " << endl;
cout << " 44 " << endl;
cout << " 4 4 " << endl;
cout << "444444 " << endl;
cout << " 4 " << endl;
cout << " 4 " << endl;
cout << " 4 " << endl;
}
void digit5()
{
cout << " 55555 " << endl;
cout << " 5 " << endl;
cout << " 5 " << endl;
cout << " 5555 " << endl; cout << " 5 " << endl;
cout << " 5 " << endl;
cout << " 5555 " << endl;
}
void digit6()
{
cout << " 666 " << endl;
cout << " 6 " << endl;
cout << " 6 " << endl;
cout << " 6666 " << endl;
cout << " 6 6 " << endl;
cout << " 6 6 " << endl;
cout << " 666 " << endl;
}
void digit7()
{
cout << " 77777 " << endl;
cout << " 7" << endl;
cout << " 7 " << endl;
cout << " 7 " << endl;
cout << " 7 " << endl;
cout << " 7 " << endl;
}
void digit8()
{
cout << " 888 " << endl;
cout << " 8 8 " << endl;
cout << " 8 8 " << endl;
cout << " 8 " << endl;
cout << " 8 8 " << endl;
cout << " 8 8 " << endl;
cout << " 888 " << endl;
}
void digit9()
{
cout << " 999 " << endl;
cout << " 9 9 " << endl;
cout << " 9 9 " << endl;
cout << " 9999 " << endl;
cout << " 9 " << endl;
cout << " 9 " << endl;
cout << " 999 " << endl;
}
Last edited on Oct 16, 2016 at 6:32pm UTC
Oct 16, 2016 at 8:03pm UTC
Have the choice() function return the digit entered by the user. Catch this return in the variable 'digit' back in main. This is the variable the switch statement will check for.
Oct 16, 2016 at 9:03pm UTC
what do you mean catch the return back in main? Sorry, I'm fairly new at this.
Oct 16, 2016 at 10:19pm UTC
1 2 3 4 5 6 7 8 9
int choice() // notice return type is no longer void, but int
{
int digit;
cout << "Enter a digit (0-9) or press X to exit: " << endl;
cin >> digit;
cin.ignore(256, '\n' );
return digit; // return this value back to main
}
in main:
1 2 3 4 5 6 7 8 9 10
int digit = 9;
banner();
digit = choice(); // put return value of choice() into 'digit'
switch (digit)
{
//....
}
Last edited on Oct 16, 2016 at 10:20pm UTC
Oct 16, 2016 at 10:31pm UTC
You can always pass by reference to access
digit directly:
choice() function:
1 2 3 4 5 6 7 8 9
// pass digit by reference so here digit is bound to the digit in main
//
void choice(int &digit)
{
std::cout <<"Enter a digit (0-9) or press X to exit: " ;
// changes the digit in main directly
std::cin >> digit;
std::cin.ignore(256, '\n' );
}
main:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
int digit{9};
banner();
// pass by reference
choice(digit);
switch (digit)
{
// .....................
}
}
Last edited on Oct 16, 2016 at 10:34pm UTC
Topic archived. No new replies allowed.