Question:
No errors but no output in C++ program?
anonymous
2009-10-30 12:42:56 UTC
Here's my code. Any help would be appreciated.


#include

using namespace std;

void get_Data();
int is_Divisible(int num1, int num2);

int num1, num2;

int main()
{
void get_Data();
int is_Divisible(int num1, int num2);
}

void get_Data()
{
cout << "Enter two integers: " << endl;
cin >> num1 >> num2;
}

int is_Divisible(int num1, int num2)
{
int remainder;

remainder = num1 % num2;

if (remainder = 0)
cout << num1 << " is evenly divisible by " << num2 << endl;
else
cout << num1 << " is not evenly divisible by " << num2 << endl;
return 0;

}
Seven answers:
The Phlebob
2009-10-30 12:51:42 UTC
No compilation errors hardly ever means the program will run correctly. The cause of your no output is these two statements in your main():



void get_Data();

int is_Divisible(int num1, int num2);



Because they have types (void and int), the compiler thinks they're function prototypes, and does not generate any executable code for them. Lose the void and int. There are other problems, but that answers your immediate question.



And yes, a statement that waits for input is a good idea right before the closing bracket in main().



And one final tip: Turn on ALL compiler messages, and fix EVERY one, no matter how trivial it seems.



Hope that helps.
Mark aka jack573
2009-11-03 08:23:13 UTC
1) You are putting the function names in the main method, you are not calling the functions.

2) Since you have num1 and num2 as global variables, you do not need to pass then into isDivsible and you don't need to return an int.

3) You should use == for comparing number. This is the if (remainder = 0) line.



Here is code that will fix all of the above problems.



#include



using namespace std;



void get_Data();

void is_Divisible();



int num1, num2;



int main()

{

get_Data();

is_Divisible();

}



void get_Data()

{

cout << "Enter two integers: " << endl;

cin >> num1 >> num2;

}



void is_Divisible()

{

int remainder;



remainder = num1 % num2;



if (remainder == 0)

cout << num1 << " is evenly divisible by " << num2 << endl;

else

cout << num1 << " is not evenly divisible by " << num2 << endl;



}





Good luck.
Kiran
2009-10-30 12:51:06 UTC
I dont know anything about C++.



But I am not sure why are you putting return type for methods when you call them. May be your main method should be something like this.



int main()

{

get_Data();

is_Divisible(int num1, int num2);

}
anonymous
2009-10-30 12:56:07 UTC
ok so yes it is going to fast for you to view it... though it is working..

do not use system ("Pause")(that will make your programs only work on windows) instead use the cin.get(); function which will require enter to comntinue so then you will be able to see DOS screen that pops up

you can also do cout<<"please press enter to continue"; to prompt for user input



I'm not sure if some of this is 100% correct because i haven't coded in c++ for a couple of months but it shoiuld be close
anonymous
2016-12-01 10:53:31 UTC
purely some techniques from somebody who found out C back in college and can cope with lots of C++ yet would not touch the string style if he can avert it. i do no longer think of you're examining something. this device compiles and runs with out enhancing and purely the criticism that bash won't be able to discover pause (because of the fact i'm on Linux and don't prefer to could cope with that) yet no longer purely are there purely rubbish interior the BB and playername variables, yet as quickly as I ask approximately inputFile gdb comments "incomplete style". attempt fstream inputFile with an inputFile.open("2.txt", fstream::in); additionally attempt examining till you get to the tip of document. And for debugging purposes, increment count quantity in any case yet have it print itself out each and every time it increments. to that end: count quantity = 0; mutually as (!inputFile.eos()){ inputFile >> BB[count quantity]; inputFile >> playername[count quantity]; cout << count quantity++; } those are purely techniques yet they're nicely worth paying interest to. additionally, the previous C style could be the ASCIIZ string, it is an array of chars often terminated with a null character. you ought to evaluate utilising that rather of the string style, provided that the sector names are so short. define it as a 2-dimensional array as in playername[Baseball][Baseball];
anonymous
2009-10-30 12:49:05 UTC
You are getting output, it is just that it comes and goes so fast the window doesn't get a chance to display. Put



system("pause");



as the last line in main.

========================

I bow to Phelob - good catch. Was too obvious for me to see.
Phil
2009-10-30 12:50:52 UTC
What LoverOfWine said.



Also, it should be "if (remainder == 0)".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...