My C++ Program isn't executing my cout code - c++

I am learning C++, and I am trying to make a simple program which prints 5 variables, as my book said to do this, but it is not executing my code.
#include<iostream>
using namespace std;
int main()
{
//Program Code below
return 0;
char letter; letter = 'A'; //Declared, then initialized
int number; number = 100; //Declared, then initialized
float decimal = 7.5; //Declared AND initialized
double pi = 3.14159; //Declared AND initialized
bool isTrue = false; //Declared AND initialized
cout<<"Char letter: "<<letter<<endl;
cout<<"Int number: "<<number<<endl;
cout<<"Float decimal: "<<decimal<<endl;
cout<<"Double pi: "<<pi<<endl;
cout<<"Bool isTrue: "<<isTrue<<endl;
}

As soon as your code executes this line
return 0;
no other lines of your code will be executed - from a practical point of view, your program will have ended. Move this line down so that it is the last line of code executed by your main() function.

Your problem is that you are returning from main before doing anything:
int main()
{
return 0; // HERE!!
// no code after return gets executed
}

Your return 0; should be at the end of main, not the start

Please re-position the "return 0;" statement

Since main is a function that returns an integer, the execution of the main function is primarily to return some integral value. As soon as the value is returned, the function assumes its job is complete and hence does no longer hold the control of the program.
Your code:
#include<iostream>
using namespace std;
int main()
{
return 0; // Function thinks its job is done hence it ignores everything after it.
...some other code
}
Actually what you wish to do:
#include<iostream>
using namespace std;
int main()
{
... useful code
return 0; // Okay. Returning is alright, as the useful job is done.
}

Related

For and while loop not working in a globally defined function

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int help(int i,int money,int denomination[]){
int sum=0;
int j=0;
while(i>0){
if(i&1) sum+=denomination[j];
i=i>>1;
j+=1;
}
return sum==money?1:0;
}
int ans(int numOfNotes,int money,int denomination[]){
for(int i=0;i<(1<<numOfNotes);i++){
if(help(i,money,denomination)){
return 1;
}
}
return 0;
}
int main() {
int testCases,numOfNotes,money;
cin>>testCases;
while(testCases>0){
cin>>numOfNotes>>money;
int denomination[numOfNotes];
int i=0;
while(numOfNotes){
cin>>denomination[i];
i++;
numOfNotes--;
}
testCases--;
ans(numOfNotes,money,denomination)==1?cout<<"Yes"<<endl:cout<<"No"<<endl;
}
return 0;
}
If there exists a subset of array denomination such that it amounts to money, program should show "Yes", else "No".
But for the following simple input
1
3 3
1
1
1
output is coming out to be
No
whereas it should be
Yes
According to me, the for and while loops are not working in the ans and help functions. Is there any other bug in the program?
You're modifying numOfNotes in the inner while loop in main, which means its value is 0 when later passed to ans. The loops in the functions are working, you're just giving them other limits than you expected.
You can easily solve issues like this yourself by stepping through your program in a debugger and inspecting variable values and control flow along the way. This is a vital programming skill. See also How to debug small programs by Eric Lippert.
Unrelated to the problem at hand, but please also see these SO questions about what's wrong with including bits/stdc++.h and declaring using namespace std;.

friend function not printing out what it should

whenever I run the program, there is no output, the program just ends. Am i doing something wrong? I'm sure there's something i missed but i can't seem to figure it out.
#include <iostream>
#include <string>
using namespace std;
class Addr
{
public:
Addr(int i = 0){
total = i;
}
void addNum(int num){
total += num;
}
int getNum(){
return total; }
friend int print(Addr& var);
private:
int total;
};
int print(Addr& var){
return var.total;
}
int main()
{
Addr object1;
object1.addNum(3);
print(object1);
return 0;
}
Your program behaves correctly. There is no output because you are not printing anything to the console in your program.
The print function merely returns the total.
If you wish to print the value to the console then you could for example change the definition as follows:
int print(Addr& var){
cout << var.total << endl; // this prints to the console output
return var.total;
}
There is no issue with your code. The fact is that no print function is used. I have modified your main function.
int main()
{
Addr object1;
object1.addNum(3);
cout<<print(object1);
return 0;
}

Test case not passed using while loop in C++

My Need:
Using a while loop greet as many names as that are available on the stdin. Stop when you read the string '42' as a name.
My Coding:
#include<iostream>
using namespace std;
int main()
{
int input=1;
int i= 0;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Result:
For input 42, test case is passed. For other input, test case failed. Please post your answer.
Answer After ~1 Year:
Very sorry for this question . This is I asked when I have 0 knowledge about C++. This may be useful for the freshers.
Your loop is flawed
int input=1;
int i= 0;
string name;
while(input<=i)
as input is greater than i to start with
You think the test case works for 42 but actually the logic inside your loop is never executed. It is simply the case that the console output is the same (i.e. there is none) but your code never gets as far as the cin to check the input is 42
Your code doesn't ever enter while since the condition is always false.
Just use,
....
while(1)
{
....
This will run your loop indefinitely, and break whenever 42 is encountered.
Your code does not even run when you pass 42, because input is greater than i.
while(input<=i) // input = 1, i = 0, 1 > 0
What you probably want is an infinite loop:
while (true)
Your Code has these Problems:
It won't enter the while loop as input is initialized to 1 and i is initialized to 0.While checking the condition *while(input<=i)*i.e., while(1<=0) which is false it won't execute the statements below.
-
My Solution:
#include<iostream>
using namespace std;
int main()
{
int input=0;
int i= 1;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Initializing input with 0 and i with 1 will give you the desired output.
First of all, Sorry for my basic question. The loop is not initiated. Because the input is 1 and i is 0. But the condition I given is input<=i. Because of the false condition, the control is not entering in loop

Calling a function in C++

I'm new to the whole C++ programming thing, and I know this is a easy solution, but I just cant figure it out!
I simply just wanna call a function that prints out 1 + 4.
Here's the code:
#include <iostream>
using namespace std;
int func()
{
cout << 1 + 4;
return 0;
}
int main()
{
int func();
}
It shows nothing in the console window, only that the application stopped with return code 0.
Can someone tell me what's wrong?
You are not calling func() function correctly:
int main()
{
// int func(); This line tries to declare a function which return int type.
// it doesn't call func()
func(); // this line calls func() function, it will ouput 5
return 0;
}
you can just call the function by its name. Like func();
int func()
{
cout << 1 + 4;
return 0;
}
the above function is retruning an integer. you are returning 0. to make it more useful return the sum and catch it in main function.
int func(){
return 1+4;// return 5 to main function.
}
now in main.
int main (){
int ans = func();// ans will catch the result which is return by the func();
cout<<ans;
return 0;
}
try to understand the working of each statement.

How to initialize this array in a clean way?

I am trying to initialize this array in C++ :
C++
#include<iostream>
using namespace std;
int main(){
int arr[100];
int i = 10;
while(i){
cin >> arr[--i];
}
return 0;
}
This initializes the array perfectly, but it returns a negative status. How can I solve it?
The status code means the program didn't get to the last line of your main() function (where it should be return 0), but got killed instead. I guess you just stopped it with CTRL+C.