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 2 years ago.
Improve this question
#include <iostream>
int main() {
int manic;
std::cout << "Enter Size of Array" << "/n";
std::cin >> size; // << "/n";
size = manic;
static const int arr[size];
for(int i = 0; i < size; i++) {
std::cout << "Enter" << i << "Element" << "/n";
std::cin >> (arr[i]); // Error Is Shown in this Line
}
bool r = is_even(arr, size);
std::cout << r;
return 0;
}
My first post here .I typed this code in Visual Studio 2019.The Microsfot Documents do not help.
The compiler gives you the hint that you may uninentionally override a value
std::cin >> size;// << "/n";
size = manic;
You let the user input a value and then override it with another, by the way uninitialized, value.
Related
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 1 year ago.
Improve this question
Hi I need help with this code. It keeps on printing "ur input:32765" and the number keeps changing. I read a question on stack overflow and it said it wasn't initialized, whatever that means. Can someone help with whats wrong?
#include <iostream>
using namespace std;
int main() {
int x;
cout << "ur input:";
cin >> x;
cout << "" << x;
return 0;
}
Write
if (cin >> x){
cout << "" << x;
} else {
cout << "bad input";
}
otherwise a read of an unintialised x could arise if the cin fails in C++03 or earlier. That can happen if there are not data on the stream that can be read into an int type.
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 3 years ago.
Improve this question
So I'm kind of new to c++ and I'm currently working with strings. and I want to input some amount and compare them to each other, but since i have them in data type in arrays it wont let me do the substrcution and I don't understand why
for (int i=0;i<N;i++)
{
cout << "Name"<< endl;
cin >> data[i].name;
cin >> data[i].all;
cin >> data[i].con;
}
exceed = data[i].con-data[i].all;
while (exceed > maxvalue){
maxindex = -1;
maxvalue = exceed;
if (maxvalue > 0){
cout << data[i].name;
}
Without knowing what type or struct or class you're using for your data member, or what error you're encountering it's hard to tell you what exactly is going on. You are also referencing i outside of your for loop so that may be your issue.
I've recreated a short program that seems to be doing what you're going for with a simple struct. Because the struct defines con and all as int types, they are converted on input, and i is no longer referenced outside of the for loop.
#include <iostream>
#include <string>
struct dataType {
std::string name;
int all;
int con;
};
int main() {
int N = 2;
int maxValue = 3;
dataType data[N];
for (int i = 0; i < N; ++i) {
std::cout << "Name" << std::endl;
std::cin >> data[i].name;
std::cin >> data[i].all;
std::cin >> data[i].con;
int exceed = data[i].con - data[i].all;
if (exceed > maxValue) {
std::cout << data[i].name << std::endl;
}
}
}
If you are using a struct or something where con and all are strings, there is a method in std::string stoi that can convert string types to int. Below is a short example.
int x;
std::string test = "4";
x = std::stoi(test);
std::cout << x << std::endl;
Note that an invalid argument in stoi throws an exception, but as a beginner you probably haven't learned about exception handling yet (but you should once you get the hang of things).
Hope that helps, cheers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
I'm new to programming and i have problem with some items
i would appreciate any help
first i started initializing the vector as followed but i couldn't end the loop with Ctrl+Z
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <double> temps;
double temp;
cout << "Enter a sequence of tempreatures : " << "\n" ;
while (cin >> temp){
temps.push_back(temp);
}
double sum = 0;
for (int i = 0; i< temps.size(); ++i)
sum += temps[i];
cout << "Mean temprature : " << sum / temps.size() << "\n";
sort(temps.begin(), temps.end());
cout << "Median temprature : " << temps[temps.size() / 2];
then i changed the while into this format :
cout << "ENter a sequence of tempreatures ending in 1500 : " << "\n" ;
while (cin >> temp){
if (temp == 1500)
break;
temps.push_back(temp);
}
now i have this error
"vector subscript out of range"
apparently break does not work properly here
what should i do?
Your issue is in the check condition of for loop.
for (int i = 0; i, temps.size(); ++i)
sum += temps[i];
It should be
for (int i = 0; i < temps.size(); ++i)
i, temps.size() will evaluate and then ignore the part before , and are left with temps.size() as check condition which will always be greater than 0 if you push_back at least one element and your loop will never end.You might want to read how ,(comma) works.
If you switch to std::getline into a string instead of std::cin into a double, you can check whether the input is empty:
std::string input;
std::getline(std::cin, input);
while (!input.empty()){
temps.push_back(atof(input.c_str()));
std::getline(std::cin, input);
}
If you also fix the for-loop as mentioned by Gaurav Sehgal, it works fine (Enter all numbers then hit enter without any input).
If you are on windows then you have to do
CTRL + Z
If you are on Unix based(Linux/Mac) then you have to do
CTRL + D
This will give the end of file signal and you will be able to break the loop
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
Every time I run this code in Visual Studio 2015 it shows the error identifier "treasureLocation" is undefined... what am I doing wrong?
int main()
{
int gridSize [2];
int gridX = 0;
int gridY = 0;
int treasureLoaction[2];
int end;
std :: cout << "what size grid would you like to play on?" << std :: endl;
std :: cin >> gridSize [1];
std :: cin >> gridSize [2];
treasureLocation[1] = rand() % gridSize[1] + 1;
treasureLocation[2] = rand() % gridSize[2] + 1;
while (gridY < gridSize[2]) {
gridY++;
while (gridX < gridSize[1]) {
std::cout << "* ";
gridX++;
}
std::cout << "" << std::endl;
gridX = 0;
};
std::cout << treasureLoaction;
std::cin >> end;
return 0;
}
There is a typo
int treasureLoaction[2];
^^^^^^^^
Take into account that if an array has n elements then the valid range of indices is [0, n-1]
And this statement
std::cout << treasureLoaction;
does not outputs the elements of the array as you might think.
You could do it in a loop as for example
for ( int x : treasureLocation ) std::cout << x << ' ';
std::cout << std::endl;
Try iterating through the array to print its contents like this
for (int i = 0; i < your array length; i++) cout << array[i];
unless you are trying to print the address, and it also looks like you have a spelling error in treasureLoaction.
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
I need to write my own sqrt function: double my_sqrt_1(double n)
How would I go about doing this? At first I tried putting this outside of "int main()":
double my_sqrt_1(double n)
{
int x = 1;
x = (x + n / x) / 2;
}
I then put this:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << x;
}
I also tried:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << my_sqrt_1;
}
None of this worked though. I'm probably doing this completely wrong, but it made sense in my head.
"I'm probably doing this completely wrong ..."
Sorry to say that, but yes.
You need a variable to receive input, and call your function passing that variable
int main() {
cout << "Please enter a value ";
double myNumber;
cin >> myNumber;
cout << '\n' << my_sqrt1(myNumber) << endl;
}
Also your function is supposed to return the result of the calculation
double my_sqrt_1(double n) {
double x = 1.0;
// ^^^^^^ ^^
x = (x + n / x) / 2.0;
// ^^
return x; // <<<<<<<<<<<<<<
}