function definitions
Ok I had a program that asked the user for a number. Then displayed the multiplication table for that number 0-10. It used the function prototype and code the function definitions
int getNumber( );
void displayTable(int);
and it used a loop to calculate and display the table. I have the .exe file but I lost the .cpp file :( and I need to edit it. So can anyone help me make it again since I can't edit it as a .exe file
Thank you
# 1 Re: function definitions
I suggest you write the program over again. It will not take you long time, will it?
Laitinen
# 2 Re: function definitions
right now it would because I did it a while back and forgot most things about functions
# 3 Re: function definitions
right now it would because I did it a while back and forgot most things about functions
You mean that you have forgotten most things about programming in general?
Was your program a console application?
Laitinen
# 4 Re: function definitions
well not the basic programming things, just the function definitions and protypes things. and the program was made in Dev-c++ complier
# 5 Re: function definitions
Give it a try and post your specific problems here
# 6 Re: function definitions
Give it a try and post your specific problems here
Okay what do I do from here?
#include <iostream>
using namespace std;
//prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
//call functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
# 7 Re: function definitions
Good, this is a start!
Your next steps would be to write the implementation of the getNumber() and displayTable(int) functions.
The first function should ask the user to input an integer, and then return this integer. Do you know how to prompt the user to type an integer, and how to read that integer in from the user?
To the displayTable function you should pass the integer that getNumber returned. Then you need to write a loop that prints the result of that number multiplied by 0 till 10.
Laitinen
# 8 Re: function definitions
would that go under the
system ("pause");
return 0;
}
?
# 9 Re: function definitions
would that go under the
system ("pause");
return 0;
}
?
Most people like to put their functions below the main function, but its just whatever you prefer. Start with something like this
#include <iostream>
using namespace std;
//prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
//call functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
int getNumber()
{
}
void displayTable(int a)
{
}
# 10 Re: function definitions
is this right?
#include <iostream>
using namespace std;
//prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
//call functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
int getNumber()
{
char number;
cout<<"Enter a number"<<endl;
cin>>number;
return number;
}
void displayTable(int a)
{
}
# 11 Re: function definitions
I would have to have a switch right?
# 12 Re: function definitions
#include <iostream>
using namespace std;
//Prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
//Call Functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
int getNumber()
{
char number;
cout<<"Enter a number"<<endl;
cin>>number;
return number;
}
void displayTable(int a)
{
switch (a)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case '10':
return 10;
}
}
# 13 Re: function definitions
and I would need a file to read the tables from?....
# 14 Re: function definitions
and I need help.. cause I'm stuck...
#include <iostream>
using namespace std;
//Prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
ifstream inData;
//Files being opened
inData.open("Tables.txt");
//If file does not open correctly
if (!inData.is_open())
{
cout<<"ERROR: Could not open input file for reading!\nPress any key to exit";
getch();
return 0;
}
//Call Functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
int getNumber()
{
char number;
cout<<"Enter a number"<<endl;
cin>>number;
return number;
}
void displayTable(int a)
{
switch (a)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case '10':
return 10;
}
}
# 15 Re: function definitions
First of all, using a char for number might not get you quite what you expect. When you enter 3, you're liable to return it as the ASCII value for the character '3' rather than the number 3. Use an int.
Your completely unnecessary switch statement seems to be compensating for this issue right now, but I don't know why you put it in displayTable. If you were trying to convert ASCII to int, you could just to n - '0' as easily; and you'd name the function to do it something more reasonable like int ctoi(char c). Like I said, this step is unnecssary if you cin to a variable of int type; it can figure out what you mean in that case.
Second, what are you going on about files for? Just calculate the table as you go. Computers are very good at math.
# 16 Re: function definitions
ok so what would I change here?
#include <iostream>
using namespace std;
//Prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
//Call Functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
int getNumber()
{
int number;
cout<<"Enter a number"<<endl;
cin>>number;
return number;
}
void displayTable(int a)
{
switch (a)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case '10':
return 10;
}
}
# 17 Re: function definitions
Well, I'm not going to write your code for you. However,
1) Get rid of the switch statement, I don't know what you were thinking there.
2) Add a loop i = 0 to 10 in displayTable.
3) For each iteration, output a*i.
# 18 Re: function definitions
would it be a while loop?
# 19 Re: function definitions
It could be. In this particular case I think a for loop would be easier, but in the end both do exactly the same thing; it's a matter of preference.
# 20 Re: function definitions
ok well i've never done a for loop lol so i'll stick to while loop for now
so would it look something like this?
void displayTable(int a)
{
while(i==0)
{
cout<<"0 multiplied by anything is 0."<<endl;
if(i=='1')
{
}
if (i=='2')
{
}
if (i=='3')
{
}
if (i=='4')
{
}
if (i=='5')
{
}
if (i=='6')
{
}
if (i=='7')
{
}
if (i=='8')
{
}
if (i=='9')
{
}
if (i=='10')
{
}
}
}
# 21 Re: function definitions
No. The entire function could be written in two lines: one for the while (or for), one for the output statement. You're making it more complicated than it needs to be.
And don't compare i with characters. It's an integer. In fact, the only place you should be comparing it with anything is in the loop conditional.
# 22 Re: function definitions
but would the 2nd line be an if or how would it work with 2 lines?
void displayTable(int a)
{
while(i==0)
{
cout<<"0 multiplied by anything is 0."<<endl;
}
}
# 23 Re: function definitions
Since you seem to be missing the basic concept, I'll give you this one.
for(i=0; i <= 10; i++)
cout << a << " times " << i << " = " << a*i << endl;
# 24 Re: function definitions
o now I see, cause a = the number they input right? but wouldn't need more lines to do all the multiplication tables? if user picks 1 it will show all of the 1's table and so on?
0 multiplied by anything is 0.
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 =30
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 27
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 9
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
#include <iostream>
using namespace std;
//Prototypes
int getNumber( );
void displayTable(int);
int main()
{
int number;
int i=0;
//Call Functions
number=getNumber();
displayTable(table);
system ("pause");
return 0;
}
int getNumber()
{
int number;
cout<<"Enter a number"<<endl;
cin>>number;
return number;
}
void displayTable(int a)
{
for(i=0; i <= 10; i++)
cout << a << " times " << i << " = " << a*i << endl;
}
and it tells me displayTable(table); is undecleared.. did I write it wrong oor miss something?
# 25 Re: function definitions
Well, I do see an extra close-brace in there.
# 26 Re: function definitions
ok i think i took out the right extra close-brace but it still telling me that this part is not correct? displayTable(int); - In function `int main()': expected primary-expression before "int"
//Call Functions
number=getNumber();
displayTable(int);
system ("pause");
return 0;
}