Jupyter notebook error for C++ Kernel[cling] - c++

I have installed cling kernel for using C++ in Jupiter notebook
but after implementing the code
#include <iostream>
using namespace std;
int main() {
int a;
a=9;
cout<<a;
return 0;
}
I am getting an error as ---> error: function definition is not allowed here
int main() {

In cling you don't write the whole program code. It's like a script language. You just write the lines that should be evaluated. Don't write the main function:
#include <iostream>
using namespace std;
int a;
a=9;
cout<<a;
You can also define functions in cling but then you are not allowed to write other code into the same cell.

Related

When running in local ide all outputs are correct but while submitting in codeforces it is throwing error

Following is the code which is running as expected in local ide but when i am submitingh the code in codeforces it is displaying that the outputs are wrong.Question is enter link description heredense array
#include<bits/stdc++.h>
using namespace std;
int main()
{ int t;
cin>>t;
while(t--)
{
std::vector<int> my;
int x,i,a,j,bno,sno;
cin>>x;//enter number of elements
for(i=0;i<x;i++)
{
cin>>a;//enter element
my.push_back(a);
}
for(i=1;i<my.size();i++)
{
for(j=i-1;j<my.size();j++)
{
bno=(my[j]>my[j+1])?my[j]:my[j+1];
sno=(my[j]<my[j+1])?my[j]:my[j+1];
if(bno>(2*sno))
{
auto it=my.begin()+j+1;
my.insert(it,(2*sno));
break;
}
}
}
for(i=0;i<my.size();i++)
{
if(my.back()==0)
{
my.pop_back();
}
}
cout<<(my.size()-x)<<endl;
}
}
Your problem is the following:
#include<bits/stdc++.h>
This is a header-file created by Bjarne Stroustrup(the creator of C++) and is only meant for beginners to use(so that they don't have to pick the headers they need to include).
Simply replace it with:
#include <iostream>
#include <vector>
On a related note: You seem to be a total beginner to c++(e.g. You are using using namespace std;, which is bad practice; google it). You really shouldn't be doing competitive programming. Better start with the basics.

Trying to implement a function C++

I started learning C++ a few weeks ago. Now I'm trying to program a kind of shop as a challenge. I've made it 2 or 3 times before, but always in one program. This time I tried to put some functions I wrote in it, so the main file wouldn't be that messed up again.
The problem I'm having is, when I'm trying to import a function, I get this error message:
E0413 There is no suitable conversion function from 'std::basic_ostream<char, std::char_traits<char>>' to 'int'.
Here's the code:
Mainfile:
#include <iostream>
#include "Benutzer.h"
using namespace std;
int main()
{
user;
}
Function:
#include <iostream>
using namespace std;
int user
{
cout << "So you're a user. What do you want to buy?"
}
I know it's not much code by now, but I was already testing.
As I can see, your program has an error in syntax.
Two things you need to consider here:
How to define a function:
return_type func_name(data_type args){
/// function body
}
How to call a function:
func_name(args);
I verified your code with little change on my system.
This is correct code:
main_file.cpp
#include <iostream>
#include "Benutzer.h"
using namespace std;
int main()
{
user();
}
Benutzer.h
#include <iostream>
using namespace std;
int user()
{
cout << "So you're a user. What do you want to buy?";
return 0;
}
This works.

Using GetCpInfo

I am trying to write a small program to run GetCpInfo, but am getting an identifier not found error . I am including windows.h and using visual studio. IntelliSense is autocomplete for me when I type in GetCp. Here is my code.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
LPCPINFO cpinfo;
cout << "Hello World!" << endl;
bool test = GetCpInfo(37, cpinfo);
int x;
cin>>x;
return 0;
}
Two problems:
The function is named GetCPInfo. Remember that the language is case sensitive.
You are passing an uninitialized pointer.
You need the following:
CPINFO cpinfo;
bool succeeded = GetCPInfo(37, &cpinfo);

Why does my simple C++ project terminate in eclipse before I can see it? How do I get this to stop?

I am new to c++, but know a little java so use words I will understand. I am using MinGW.
This is my code:
#include <iostream>
using namespace std;
int main()
{
cout <<"C++ is fun!";
return 0;
}
Add
system("pause"); // because you want it to pause
before
return 0;

Compile-time error: Multiple definition of 'main'

I am getting the following error: Multiple definition of `main'
I have created a new project, there are two c++ files in it:
File 1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
//fflush(stdin);
//getchar();
return 0;
}
File 2
#include <iostream>
using namespace std;
int main()
{
cout<<"Demo Program";
return 0;
}
When I press Build project and Run, I get error. How do I run these files?
You cannot have two main functions in the same project. Put them in separate projects or rename one of the functions and call it from the other main function.
You can never have more than one main() function in your project since it is the entrypoint, no matter what the parameter list is like.
You can however have multiple declarations of other functions as long as the parameter list is different (function overloading).
File 1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
otherFunction();
return 0;
}
File 2
#include <iostream>
using namespace std;
void otherFunction()
{
cout<<"Demo Program";
}
Dont forget the appropiate #includes.
You can't have two main functions. In fact you can't have any two functions having the same signature through out your project (not your files).
And as Mr.TAMER said main is a special case, you can't even have two functions called main.
Decide which file you want to be as entry point of your project.
In other file, change the method name to some other name. you can call it from the file you chose at step 1.
main is entry point of your program and you can't have more than one entry point.
For more clear explanation see this: Two 'main' functions in C/C++
You can not use the same function signature in a same project,becouse the compiler start execution from the main(). If you define multiple times of main() then it produce an error.
file1.c
#include <iostream>
#include <file2.h>
using namespace std;
int main()
{
cout<<"Hello World";
//fflush(stdin);
//getchar();
return 0;
}
And in file2.h,you can define the function of file2.c(first rename the main() of the file2)