Initialization of 1D array [closed] - c++

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 month.
Improve this question
I have this simple code and I need to initialize the Range[] array, but I get this error:
>gcc c.cpp c.cpp: In function 'int main()': c.cpp:16:7: error: expected primary-expression before ']' token
Range[]={12,22,35,45,69,74,79,78,66};
How can solve it please?
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;
using std::string;
int Range[8];
int main(){
Range[]={12,22,35,45,69,74,79,78,66};
for(int i=0;i<8;i++){
cout<< Range[i] <<endl;
}
return 0;
}

What you're doing is actually assignment and not initialization.
And you can't assign to an array, just copy to it.
The solution? Do actual initialization:
int Range[]={12,22,35,45,69,74,79,78,66};
Note that since I didn't specify a size for the array, the compiler will automatically set the size to nine elements, as in the initializer list. Unlike your current definition of the array where you set the size to only eight elements.
I also recommend you make the variable a local variable inside the main function. Global variables are to be avoided.

Related

How do I make pass and fail count work in c++? [closed]

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 5 months ago.
Improve this question
I'm working on this function for one of my classes and my pass count works just fine, however, my fail count ALWAYS prints out 12. I've been reading my code top to bottom and just can't seem to find the problem.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string passAndFailCount(string grades){
int pass_count;
int fail_count;
istringstream myStream(grades);
string grade;
while(myStream>>grade){
int i_grade=stoi(grade);
if (i_grade>=55){
++pass_count;
}
else{
++fail_count;
}
}
cout<<"Pass: "<<pass_count<<endl<<"Fail: "<<fail_count<<endl;
}
int main()
{
string grades;
getline(cin, grades);
passAndFailCount(grades);
}
Your problem are uninitialized variables.
int pass_count = 0;
int fail_count = 0;
and you're set.
For an explanation. Non-global variables (which automatically get initialized to 'default' (0) as per the standard), automatic and dynamic variables (like the one you are using), assume the value of 'whatever was in memory at the time of allocating the variable'.
Memory is never empty, there's always 'something' written in there.
So the memory of your fail_count variable just 'happened to be' 12 during allocation, which is why you start with that value. This value can be anything within scope.
By explicitly assigning a variable, you 'initialize' the variable, putting it into a defined state (0) and your program should start working as expected.

(cin >> int x ) showing error. why can't declare variable after cin [closed]

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
A very short program
#include <iostream>
using namespace std;
int main()
{
cin>> int x;
return 0;
}
it's not compiling, why can't declare after cin?
You first have to declare a variable before using it. Or in other words: Variables have to be in scope when using them. That's the way it works in C++.
cin is the object in c++ of class istream.
It is Used to accept the input from input devices like keyboard and
int x; is declaration of variable i.e. allocating the space where our number from input stream will get stored.>> is extraction operator which receives the stream.So declaration and taking input are 2 different things. 1) allocate memory(int x)2)write into it.
These 2 things won't happen together.

cout was not declared in this scope [closed]

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'm new to C++ and coding entirely.
When I try to build my code it gives me "error: 'count' was not declared in this scope"
Everything I look up either tells me to add "using namespace std;" or add "int main()" but neither works for me.
#include <iostream>
using namespace std;
main()
{
int A = 4;
count << &A;
}
There is a typo in your identifier.
count should be cout.
Also, main should have the return type of int as it isn't standard C++ to automatically deduce the return type as int if not specified. In short, int main() is required.

casting a vector's size() as int does not work [closed]

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
I am learning C++ on the fly and am having problems with vectors so I am writing some programs that use vectors to familiarize myself with them.
I was following the advice from this post regarding printing out the value of a vector's size() call:
How can I get the size of an std::vector as an int?
My code is a simple C++ code:
#include <vector>
int main(int argc, char ** argv) {
/* create an int vector of size 10, initialized to 0 */
std::vector<int> int_list[10];
int int_list_size;
int_list_size = static_cast<int>(int_list.size()); // <-- compilation error here
} // End main()
I'm on Ubuntu 16.04 and I get this error:
"error: request for member 'size' in 'int_list', which is of non-class type 'std::vector<int> [10]'
Since the size of the vector int_list is 10, shouldn't size() return 10, which I can then cast to an int?
You are not creating a vector, you are creating an array of vector:
std::vector<int> int_list[10];
You should use:
std::vector<int> int_list(10);
See:
https://www.cplusplus.com/reference/vector/vector/vector/
https://en.cppreference.com/w/cpp/container/vector/vector

Getting iterator for an array with a constant length in c++ [closed]

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 5 years ago.
Improve this question
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);