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--) {
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 8 months ago.
Improve this question
I am a beginner, and I am trying to learn C++.
For now, all I am trying to do is input 3 numbers, and print them back.
#include <iostream>
using namespace std;
int main(){
int n[2];
cout << "Enter three numbers" << endl;
for (int j = 0; j <= 2; j++){
cin >> n[j];
}
cout << "Debug " << n[2] << endl;
cout << endl;
for (int i = 0; i <= 2; i++){
cout << n[i] << "\t" << i << endl;
}
return 0;
}
Every time I print them, the last value of the array is modified, and I cannot figure out why! For a test input 6,7,8, the output is in the image below.
This for
for (int j=0;j<=2;j++){
cin>>n[j];
}
expects that the array has at least three elements with indices in the range [0, 2].
However you declared an array with two elements
int n[2];
If you are going to input three elements then the array should be defined as
int n[3];
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 last year.
Improve this question
int nums [] {1,2,3,4,5,6,7,8,9,10};
int main(){
int size {10};
for (int i;i<size;i++)
cout << nums[i] << " ";
for (int i;i<size;i++)
cout << nums[i] << " ";
return 0;
}
This prints my array once but shouldn't I get my array printed twice not once?
You need to initialize the i variable to the start index, like in for (i = 0; i < size; i++). Otherwise i can start in any value and this is 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 6 years ago.
Improve this question
As soon as I enter the string and press enter the compiler shows debug error and says abort() was encountered.
What is actually wrong?
#include<iostream>
#include<string>
using namespace std;
int main()
{
std::string str;
std::string rev;
std::cout << "Enter the string\n";
std::getline(std::cin, str);
int len = str.size();
for (int i = len; i > 0; i--)
{
std::string temp;
temp= str.at(i);
int j = 1;
rev.insert(j, temp);
j++;
}
std::cout << "The reversed string is\n";
std::cout << rev;
cout << "Thank You";
cin.get();
}
for (int i = len; i > 0; i--)
Should be
for (size_t i = len - 1; i >= 0; i--)
// ^^^ ^
The statement
temp= str.at(i);
will be out of bounds for the first iteration otherwise.
Indices in c++ are in the [0 ... (size - 1)]range.
The earlier answer points out the problems with str.at (i). There's another: on the first iteration it calls rev.insert (1, ...). Since rev has, at that point, a length of zero, that is an out-of-bound access, which will cause an out_of_range exception to be thrown, terminating your program.
Also, move the declaration of j out of the loop. Now it gets recreated with value 1 each time through.
Using an entire string object for tmp seems overkill. Indeed, rev += str.at(i); could replace the entire loop body.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
int main()
{
vector<int>numbers;
int numb = 0;
int i = 0;
int j = i - 1;
while (cin >> numb)
{
if (numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
numbers.push_back(numb);
cout << "numbers[" << i << "] = " << numbers[i] << endl;
++i;
}
/*** I don't understand why a exception, run time error, break or whatever it names.....................................................
On the first iteration through the loop, j is -1. Accessing numbers[-1] is undefined behavior because the index is outside the bounds of the vector.
Indeed, accessing any index is out of bounds until you put something in the vector, so you cannot index numbers at all until you have called push_back on it at least once.
This code will display the message if the user enters a number already in the vector:
while (cin >> numb) {
vector<int>::iterator found = std::find(numbers.begin(), numbers.end(), numb);
if (found == numbers.end()) {
cout << "Numbers repeated" << endl;
}
// If you don't want duplicate numbers in the vector, move this statement
// into an "else" block on the last "if" block.
numbers.push_back(numb);
}
This code on the other hand will only display the message when a number was the same as the last number entered, that is, if sequential numbers are the same:
while (cin >> numb) {
if (!numbers.empty() && numb == numbers.back()) {
cout << "Numbers repeated" << endl;
}
numbers.push_back(numb);
}
You need to initialize your numbers vector with initial data, or check to make sure j is withing in vector size. A vector is essentially an array, and like any array, you cannot go out of bounds.
if (j < numbers.size() && numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
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
Here are the instructions:
Write a program that accepts an integer input from the keyboard and computes the sum of all the integers from 1 to that integer. Example, if 7 were input, the sum: 1 + 2 + 3 + 4 + 5 + 6 + 7 would be computed. Use a while or for loop to perform the calculations. Print out the result after the sum has been calculated. NOTE: if you input a large integer, you will not get the correct results.
What I can't figure out is how to ADD all of the integers together. Any help would be greatly appreciated.
//preprocessor directives
int main ()
{
//declare and initialize variables
int n, i;
int total;
//user input
cout << "Enter an integer: ";
cin >> n;
//compute sum of all integers from 1 to n
total=0;
for (i = 1; i <= n; i++)
cout << i;
return 0;
}
You add by using the += operator:
for (i = 1; i <= n; i++)
total += i;
cout << total;
Note this is short for total = total + i.