Can someone help why my array is showing junk values on print
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main()
{
int arr[] = { };
int i = 0;
int x = 0;
cout << "Please enter array values use 9999 to terminate entering values \n";
while(x != 9999){
int j = 0;
cin >> x;
arr[j] = x ;
//* cout << arr[i];
j++;
}
for (int k=0;k<5;k++){
cout << "printing char" << k << ";" << arr[k] << "\n";
}
L8. The value of j is reset to 0 every time around the while loop. Remove L8 and use i instead in L10 L12. Also note that for standard C++, arr needs to have the size declared at compile time. Note that this can be shortened to :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
constexpr size_t MaxArr {100};
int main() {
int arr[MaxArr] { };
size_t e {};
std::cout << "Please enter array values use 9999 to terminate entering values\n";
for (; (e < MaxArr) && (std::cin >> arr[e]) && (arr[e] != 9999); ++e);
for (int k = 0; k < e; ++k)
std::cout << "printing char" << k << "; " << arr[k] << '\n';
}
1. Line 3 creates an array with one element. It errors in Visual Studio 2022. "an empty initializer is invalid for an array with unspecified bound". The empty initializer in GCC/MinGW doesn't assign any value to arr[0], so it is a junk value.
2. Every pass through your while loop j keeps getting reset to 0, the loop keeps assigning whatever the user enters into arr[0].
3. You are going out of bounds with your array in your for loop, junk values past arr[0].
What is your compiler? It isn't doing you any favors by letting your code compile, with or without warnings.
With C++ conformance turned on, it is an error everywhere.
Something I am unable to do within the Code::Blocks IDE, unfortunately.
At least I haven't been able to find how to do it. That is why I stick with VS as my main development environment, with some occasional testing of already VS tested code in C:B.
Easy to do with VS since I believe conformance is the default. I know every new project/solution I create it is set.
With Code::Blocks IDE, to turn on C++ conformance globally (as the default for all projects),
Menu => Settings => Compiler... => Global Compiler Settings
Tick the following flags: -std=c++20 -pedantic-errors
Good idea to also tick: -Wall -Wextra
So much C++20 functionality is missing/half-supported with GCC vs. VS. Modules, std::format (I can't find <format> in any of the various standalone GCC installs I have), etc. TDM-GCC, MinGW, Clang, MSYS2....nada. ::sads::
In VS, watch out for Properties/C++/Language/Disable Language Extensions. If set to yes then windows.h doesn't compile!
Heh, don't I know it! After setting Disable Language Extensions globally in VS for C++ console conformance the few times now I create a new WinAPI project that global setting bites me in the tuchas. Until I remember and reset the settings for WinAPI.
Even with VS 2022 (same for VS 2019) to get a large part of C++20 functionality to work the language standard set has to be -std:c++latest. So I simply set that as global setting a while back.
[ETA]: We've hijacked the OP's thread more than enough, hopefully we (mostly ME) didn't scare them away with the side chatter.
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
vector<int> arr; // if you want to start with an empty array
cout << "Please enter array values. Use 9999 to terminate entering values\n";
for ( int x; cin >> x && x != 9999; )
{
arr.push_back( x );
}
for ( int k = 0; k < arr.size(); k++ ) // introspective vector method size()
{
cout << "Printing element " << k << ": " << arr[k] << "\n";
}
}
Please enter array values. Use 9999 to terminate entering values
10 20 30 9999
Printing element 0: 10
Printing element 1: 20
Printing element 2: 30
Atom using GPP invokes MinGW. Chances are when you compile you are not requiring strict C++ conformance, so what should be errors are not getting flagged as errors. Line 3, for instance.
Logical errors like constantly resetting j to 0 at the top of the while loop isn't a C++ error, so at best you MIGHT get a warning about stuffing the input into arr[0] repeatedly.