Any cout statement inside main function is not printing any value during debugging but cout inside other functions are printing values during function call (I use vscode )
#include<iostream>
using namespace std;
int main()
{
int a;
a=9;
if(a==9)
{
cout<<"hello";}
return 0;
}
when this is debugged placing a breakpoint on the first line of main()
"hello" is not printed in the debug console.
#include <iostream>
using namespace std;
void fun(int n)
{
if (n > 0)
{
cout << n << endl;
fun(n - 1);
}
}
int main()
{
int x = 3;
fun(3);
cout<<x;
return 0;
}
but when this is debugged by placing a breakpoint in the first line of main()
values are printed like
3
2
1
std::cout is buffered. Try to use std::flush or std::endl.
Also you can try to use std::cerr instead std::cout.
Related
I've read that an #include header.h is a preprocessor (because of #), which means it gets processed before compilation.
Is that why my code can't run? Because I'm trying to make an if statement in main with my function from my header(that takes a parameter) and it won't work.
Source.cpp
#include <iostream>
#include "Header.h"
using namespace std;
int main(){
test(46);
if (test() > 30){
cout << "great";
}
else{
cout << "It needs to be higher";
}
system("PAUSE");
return 0;
}
Header.h
using namespace std;
int test(int x){
return x;
}
That isn't the problem. I suspect you might be getting a compiler error message (or linker error) because you have declared test(int x) with an integer parameter and then you call it with no parameter, e.g.: test().
I've modified your code to include an integer result:
int main(){
int result = test(46); // Save the result of calling the function
if (result > 30){ // Test the value of the result
cout << "great";
}
else{
cout << "It needs to be higher";
}
system("PAUSE");
return 0;
}
The test function in Header.h file takes a int as parameter.But in your code you lose it.Pass a int to test function like this.
if (test(42) > 30)
You will get the output: great.
I recently started learning C++ but I came across a problem. The program given below is not giving me the desired result as I only see 'Hi' in the result but not what's written in the void function. Please tell me the reason that this is happening along with the solution.
I am using Xcode 6.3.1 and the I have selected the language C++.
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
void ABC ();
return 0;
}
You are redeclaring a void ABC() function inside main(). Just call ABC(); without the void.
You can take a look at this question about declaring a function within the scope of another.
In your code your function call was wrong.
When you call your function you don't need to add the return type:
#include
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
ABC ();
return 0;
}
you need to call your method and not declare it inside main
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main ()
{
cout << "Hi \n";
ABC ();
return 0;
}
EDIT 1:
Since you started learning C++ i recommend the following recommendations to make sure your code is cleaner. Please note , these are not rules by any mean , but more of best practices and a style of coding.
Use meaningful names for your variables, methods, functions , classes
... So instead of ABC() name it something that if you (or someone
else is reading it) will now what it suppose to do.
When calling methods and functions try to declare them with the
appropriate returning value. Void by definition doesn't return any
value it just process the code inside of it. so your methods/function
should return appropriate values of do what it suppose to.
Here's version 2 of your code with examples of 3 different methods and calls:
#include <iostream>
using namespace std;
int sum;
string MethodReturningString()
{
return "Hey there i am the result of a method call !";
}
int MethodReturningInt()
{
return 5;
}
void CalculateSum(int x,int y)
{
sum=x+y;
}
int main()
{
cout << MethodReturningString() << endl;
cout << MethodReturningInt() << endl;
cout << "Calculating sum:" ;
CalculateSum(5,4);
cout << sum << endl;
return 0;
}
Happy coding
In C++, like pretty much any other language, you do not specify the return type when calling a function. So change the line that reads:
void ABC ();
to:
ABC();
Try this:
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
ABC();
return 0;
}
You should call a function simply by stating its name and adding parentheses.
instead of using void ABC() for calling the function ABC() in main(), use the following code:
#include
void ABC ()
{
cout << "Hey there ! \n";
}
int main ()
{
cout << "Hi \n";
ABC ();
return 0;
}
I have a doubt in the following code. Can somebody please explain.
using namespace std;
#define INT_SIZE 32
#define R 4
#define C 4
#define N 4
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#include<stack>
#include<vector>
#include<algorithm>
struct interval{
int start;
int end;
};
bool compareInterval(interval i1, interval i2)
{
return (i1.start < i2.start)? true: false;
}
int merge(vector<interval>& a, int n)
{
stack<interval> s;
sort(a.begin(), a.end(), compareInterval);
s.push(a[0]);
int i=1;
interval temp;
while(i<n)
{
temp = s.top();
s.pop();
if(temp.end > a[i].start && a[i].end > temp.end)
{
temp.end = a[i].end;
s.push(temp);
}
else if(temp.end < a[i].start)
{
s.push(temp);
s.push(a[i]);
}
i++;
}
while(s.size())
{
temp = s.top();
cout << temp.start << " ";
cout << temp.end << "\n";
s.pop();
}
return 0;
}
int main()
{
interval intvls[] = { {6,8}, {1,9}, {2,4}, {4,7} };
vector<interval> intervals(intvls, intvls+4);
for(int i=0;i<4;i++)
{cout << intervals[i].start;
} // This output is not coming when merge function is called
cout << merge(intervals, 4);
}
My doubt is "When I comment the merge function call i.e
// cout << merge(intervals, 4);
When I comment this line, then I am able to see output of cout<<intervals[i].start.
Otherwise, I am not able to see the output.
"
You are not ending your output with a newline. Try:
{cout << intervals[i].start << "\n";}
Without the newline, this output is probably getting hidden amongst all the output produced by merge().
There are few issues with this code.
s.push(a[0]); <-- Only on element is in you stack.
//s.push(a[1]).. you will have to add all other elements like this.
while(i<n)
{
temp = s.top(); <-- for second i s will be empty. Here you must check if stack is empty before getting top element.
s.pop();
}
Your first output is not printed correctly. use std::endl
cout << intervals[i].start << endl;
There is a bug in merge that leads to a segmentation fault. Due to this std::cout is not flushed. If you comment out the line with the merge call, std::cout is flushed when the program exits.
I am doing a buffer overflow problem and I trying to print hello world. Below is my code to
but I am getting a segmentation 11 issue when I run this file with another one. "./executable < input.cpp(This is the file below). I am doing something wrong to solve a buffer overflow issue?
#include<stdio.h>
using namespace std;
main()
{
printf("A");
//00000b00
for (int i = 0; i < 4; i++)
printf("%c%c%c%c",0x00,0x0b,0x00,0x00);
}
Below is the actual code that I am trying to print hello world. Above is my input string.
#include <iostream>
using namespace std;
int i;
unsigned int* p;
void f1() {
int a=10;
char str[4];
cout << "Please enter a string:";
while (!cin.eof()) {
cin.get(str[i]);
i++;
}
printf("address of str is:%x\n",str);
cout << "The string you entered is:";
printf("address of a is:%x\n",&a);
cout << str << endl;
}
void f2()
{
cout << "Hello World!\n";
}
main()
{
printf("The address of function f2:%08x\n",f2);
f1();
}
I am getting a segmentation 11 issue when I run this file with another one.
./executable < input.cpp
I am doing something wrong to solve a buffer overflow issue?
Yes. Buffer overflow attacks don't work like that - dumping a bunch of C source code into memory does not magically make the machine compile and run it. To generalize wildly, the data you dump into memory must contain:
Padding to force the following data to lie in the right place in the stack
A replacement address in the location of the old return address, pointing to the following executable code
Some more padding, usually a "NOP slide"
Some executable code
Please read the classic "Smashing the stack for fun and profit", and keep in mind that you may have to disable some protections (non-executable stack, ASLR, stack canary) to get these exploits to work on a modern system.
Use the %x modifier to print hexadecimal values
If this is a C program, then the use of namespace std makes no sense
#include<stdio.h>
int main(void)
{
puts("A");
for (int i = 0x0; i < 4; i++)
printf("%x\n", i);
return 0;
}
Op's post was updated:
#include<stdio.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int i = 0; //Initialise i
void f1() {
int a=10;
char str[4];
cout << "Please enter a string: ";
while (!cin.eof() && i < 4 ) { //Have a condition on length of string
cin.get(str[i]);
i++;
}
str[i] = '\0'; //Set the eof character at end of the string
printf("address of str is: %p\n", str);
printf("address of a is: %p\n", &a);
cout << "The string you entered is: " << str << endl;
}
void f2() {
cout << "Hello World!\n";
}
int main()
{
printf("The address of function f2: %p\n", f2); //To print address use the %p option
f1();
return 0;
}
The following code throws the exception perfectly fine in Visual Studio 2010:
#include <iostream>
#include <cmath>
using namespace std;
int perfectSquare(double sq, int nu);
int main()
{
double num;
double squareRoot;
int perfectSq;
cout << "Enter the a number: ";
cin >> num;
try
{
squareRoot = sqrt(num);
perfectSq = perfectSquare(squareRoot, num);
cout << "The square root is: " << perfectSq << endl;
}
catch(char * exceptionString)
{
cout << exceptionString;
}
cout << "BYE." << endl;
// system("PAUSE");
return 0;
}
int perfectSquare(double sq, int nu)
{
int temp = sq;
if (sq != temp) //clever test; if square root IS NOT an INT
{
throw "not a perfect square.\n";
}
else
{
return sq;
}
}
However, in Xcode, it will not resume and it keeps hitting a breakpoint in the debugger. For example if I inpute 33 (not a perfect square), the following error is displayed:
libc++abi.dylib: terminate called throwing an exception
(lldb)
It should "throw" this line: "not a perfect square." and the program should terminate (like in VS 2010). I don't want to enable exception breakpoints in Xcode as I just want the program to run all the way to the end without debugging.
Thanks to all.
What you are throwing is a string literal, which in XCode seems to be a const char*, not a char*
You are not actually throwing a char *, you are throwing a const char *. Change the exception catch to
catch(const char * exceptionString)
and it should work.
All literal strings in C++ are equivalent to pointers to a constant string, i.e. const char *.