For my homework I have to write a c program that contains three functions: biggest(int, int,int), input() and output(int). Here is my code. The compailer doesn't register any sintax errors but when i run the program I get output 29 even though I didn't enter that number.
#include <iostream>
using namespace std;
#include <string>
string Input();
int Biggest (int a,int b,int c);
void Output(int biggest);
int main() {
int a, b,c;
string input=Input();
int biggest=Biggest(a,b,c);
cout<<input;
cin>>a>>b>>c;
Output(biggest);
return 0;
system("Pause>0");
}
int Biggest(int a, int b, int c) {
int biggest=a;
if (b>biggest && b>c)
biggest=b;
else if (c>biggest && c>b)
biggest=c;
return biggest;
}
string Input() {
return "Input 3 numbers: ";
}
void Output(int biggest) {
cout<<"The biggest number is "<<biggest;
}
CodePudding user response:
The three integers a,b, and c are not initialized, i.e. they hold some garbage value. Move the cout and the cin lines before you call the function Biggest and the programs works correctly.
CodePudding user response:
Is that's what you meant? Had to change not only the initializing of abc integers, but maybe the way i've made this is not suitable for your homework (included algorithm lib)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int nums[3], biggest;
void Input();
int Biggest(int nums[]);
void Output(int biggest);
int main() {
Input();
Biggest(nums);
Output(biggest);
system("Pause");
return 0;
}
int Biggest(int nums[]) {
biggest = max({ nums[1],nums[2],nums[3] });
return biggest;
}
void Input() {
cout << "Input 3 numbers: ";
for (int i = 0; i < 3; i ) {
cout << "Input your " << i 1 << " number:";
cin >> nums[i];
}
return;
}
void Output(int biggest) {
cout << "The biggest number is " << biggest << endl;
}```