getting rintime error on this it is basic - c++

why am i geting runtime error in one
thats my question i am writing this simply because it is showing to lksdjflksjflkjsdflk
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int L;
long int l;
cin>>L;
cin>>l;
long int ans=L%l;
if(L>=l)
{
cout<<ans;
}
else
{
cout<<L;
}
return 0;
}

Unless you provide the inputs for L,l and runtime error description... It's ambiguous and we have to guess.. hope you are not passing 0 to 'l'..

Related

I have just started c++ but I'm getting error in hackerRank for this code

this is the program
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
getch();
}
fatal error: conio.h: No such file or directory #include <conio.h>sdfs
getch return a value
as follows
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
char a = getch();
}

c++ reference to overloaded function could not be resolved;

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
const int size = 15;
char array1[size];
char array2[size] = "four";
cout << array2[3]='\0' << endl;
return 0;
}
Hello, I am new to C++, I am learning the C-string, my question is why
array2[3] ='\0' can not be use inside the cout, the error I get is:
reference to overloaded function could not be resolved.
It works fine if it's outside of cout. thank you :)

Having Trouble in Using is_sorted in C++

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j, t;
vector <int> v;
scanf("%d", &t);
while(t--) {
scanf("%d", &j);
v.push_back(j);
}
if(is_sorted(v.begin(), v.end()))
printf("Sorted\n");
else
printf("Unsorted\n");
return 0;
}
Here's my C++ code to check whether a vector is sorted or not. But my IDE (Code Blocks) doesn't compile it and gives the message "is_sorted was not declared in this scope". What's wrong with this code?
You need to add the compile flag.
[root#router ~]# cat t.cpp
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j, t;
vector <int> v;
scanf("%d", &t);
while(t--) {
scanf("%d", &j);
v.push_back(j);
}
if(is_sorted(v.begin(), v.end()))
printf("Sorted\n");
else
printf("Unsorted\n");
return 0;
}
[root#router ~]# g++ -o t t.cpp -std=c++11
Here is the std::is_sorted reference.

Why cant I insert values into 2D vector when I try to do it in a function in C++?

I am creating a small C++ program for homework. I im trying to populate a 2D vector but when I write matriz[iA][iB]=iNum; it gives me the error "no match for 'operator='"
#include <iostream>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <iomanip>
#include <set>
#include <vector>
#include <map>
using namespace std;
void popularMatriz(int iTamano, vector<vector<int>> *matriz){
for(int iA=0; iA<iTamano; iA++){
for(int iB=0; iB>iTamano; iB++){
int iNum;
scanf("%d", &iNum );
matriz[iA][iB]=iNum;
}
}
}
int main(){
int iTamano;
scanf("%d", &iTamano);
vector<vector<int>> matriz(iTamano, vector<int>(iTamano));
matriz[2][2]=5;
popularMatriz(iTamano, &matriz);
return 0;
}
You're passing a pointer to matriz; so is wrong use it as
matriz[iA][iB]=iNum;
I suggest you to pass it as reference; I mean, define popularMatriz() as
void popularMatriz(int iTamano, vector<vector<int>> & matriz)
and call it without &
popularMatriz(iTamano, matriz);
You're taking a pointer to matriz. Do this instead:
(*matriz)[iA][iB]=iNum;

"Expected unqualified-id" for ofstream

I wrote the following C++ program, but at the line where I used out_stream.open(), it keeps telling me that there are errors about "unknown typename 'out_stream'" and "Expected unqualified-id".
I am new to C++ and I think I just copied down the lines from my textbook, so I can't figure out where it's wrong. Please bear with me if it is a really simple mistake.
Here's my code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/math/distributions.hpp>
std::ofstream out_stream;
out_stream.open("output.txt");
int main()
{
std::cout<<"hello world!";
return 0;
}
You cannot to this
out_stream.open("output.txt");
outside of a function. Put it inside the main().
int main()
{
out_stream.open("output.txt");
std::cout<<"hello world!";
return 0;
}