Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This Code passed the Compile and run Test, but when I try to submit It in HackerRank, Its saying that it's a segmentation error.
// Sample code to perform I/O:
#include <iostream>
using namespace std;
int main() {
int n;
int i;
int a[i];
cin >> n;
for(i=0;i<n;i++)
{
cin >> a[i]; //Reading Input to STDIN
}
for(i=n-1;i>=0;i--)
{
cout << a[i] << endl; // Writing output to STDOUT
}
return 0;
}
RESULT: Runtime Error - SIGSEGV
i is not initialized so you have undefined behavior. Never use a variable before initializing it.
Use vector instead of the array:
#include <vector>
int main() {
int n;
//int i; you don't need i anymore
vector<int> a;
cin >> n;
a.reserve(n);
for(int i=0;i<n;i++)
{
cin >> a[i]; //Reading Input to STDIN
}
by using int a[i]; you have two problems:
i must be known in compile time(you cannot make it dynamic then)
you store the array in stack memory, meaning the size will be very
limited
vector is a container which allows you to resize the array anytime. It
uses heap memory for storing data.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
char x;
cout << "\n/something?(y/n): ";
cin >> x;
if(x=='y'){
int n2 = 0;
cout << "number: ";
cin >> n2;
int i = n2;
for(int i; 0<i; i--){
cout << i;
}
}
else{
system("pause");
}
How come when I run the code it doesn't count down from the number the user gave?
What is happening here is that you declare local variable i in the block which contains for loop, and then declare another local variable i in the loop itself, and the one in the loop is not initialized. It gets some "trash" value, from each it counts down. I assume you wanted to use i declated above the loop in the loop, but it is done differently. To fix this I suggest 2 most reasonable variants:
variant 1 - use i declared above the loop
int i = n2;
for(; i>0; i--) {
...
variant 2 - use loop-local i:
// just this, do not declare i above the loop
for(int i = n2; i>0; i--) {
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have simplified the code to get rid of unrelated objects. This is my code:
#include <iostream>
#include <fstream>
using namespace std;
fstream asdf;
int input;
void import_image(){
asdf.seekg(0);
char character;
for(int k = 0; k < 40; k++){
asdf.get(character);
input = (unsigned int)(unsigned char)character;
}
}
void print_hello_world(){
for(int rows; rows <= 27; rows++){
cout << "hello world" << endl;
}
cout << "goodbye.";
}
int main(){
asdf.open("abc.txt", ios::binary | ios::in);
cout << asdf.is_open() << endl;
import_image();
//cout << endl;
print_hello_world();
return 0;
}
Running this code results only in
1
goodbye.
--------------------------------
Process exited after 0.1511 seconds with return value 0
however removing double slash (simply adding cout << endl;) fixes everything. I have no idea why it happens and would like to now why is it so. I know that variable "rows" has no value, but why does printing a new line fix everything?
The new "endl"
is a great sign
that what you see,
is called "UB".
Your program has Undefined Behavior (UB) because your int rows that you use for the loop iterations is uninitialized.
By UB definition anything may happen. Activate all (sane) compiler warnings to find errors like this earlier in your development process.
Undefined behavior yield working programs by completely random changes (for example the addition of std::endl) but in the end it's undefined behavior.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
my knowledge of c++ is pitiful. I've been stumped on this simple problem for a long time and would just a point to the right the direction. The basis of the program is to have the user chose a number 1-5 and then based on their decision print out a quote that many times. So if they chose the number 4 it will display the quote 4 times.
Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int PickNumber()
{
int i;
cout << "Please Enter a Number From 1 to 5:";
cin >> i;
for (int j = 0; j < i; j++)
{
cout << "Congrats!";
}
return i;
}
int main()
{
_getch();
return 0;
}
Just to add an answer to this question already solved by #molbdnilo, #Rietty and #Gox: the PickNumber() function is not called in the main function.
The PickNumber() call just needs to be added to the main function.
int main() {
PickNumber();
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a code to subtract the adjacent elements of a vector and enter the answer into a new vector. However, my code isn't working. What exactly is wrong with it?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int length;
vector<int>values;
vector<int>values2;
cout << "Enter the length of the vector";
cin >> length;
values[0]=1; values[1]=2; values[2]=3; values[3]=4; values[4]=5;
for(int i=0; i<length; i++)
{
cout<<"Enter the " << i <<"th element of the vector";
cin >> values[i];
}
for (int i=0; i<length-1; i++)
{
values2[i]=values[i+1]-values[0];
}
return 0;
}
You need to size the vectors accordingly before accessing elements. You can do that on construction, or using resize.
vector<int>values(5/*pre-size for 5 elements*/); and similar for values2 would fix your problem.
Currently your program behaviour is undefined.
If you want to subtract adjacent elements, then shouldn't you have values2[i]=values[i+1]-values[i];?
The line of code:
values2[i]=values[i+1]-values[0];
will take the looked-at element away from the first element each time.
Did you mean:
values2[i]=values[i+1]-values[i];
?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What have I done wrong with the following code?
#include <iostream>
using namespace std;
main ()
{
int a;
int b;
int sum;
cout << "Enter first number \n";
cin >> a;
cout << "Enter second number \n";
cin >> b;
sum = a+b;
cout << "The sum of both numbers is" << sum << endl;
return 0;
}
Does the editor you are using tells errors, so the code is not executing? Or som exception rises? Or it is executing but nothing is shown? Please specify your problem accurately.
Anyway, you must use
int main ()
instead of
main()
Notice that your code returns a value. The last line of you code is:
return 0;
Thus, you must specify an int return type.
Check your initial lines with this.
#include <iostream>
using namespace std;
int main ()
{