Why I am getting garbage(unwanted) output here? - c++

Whenever I am writing this following code, I am getting garbage(unexpected) output in some online compiler, but if I use code block then getting satisfied output. So my question is why I am getting this type of output?
for example, if I input
5 7
+ 5
- 10
- 20
+ 40
- 20
then I am getting
22 1
in the code block. But in the online compiler, it's something else.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int have, n, i;
int kid=0;
cin>>n>>have;
int line[n];
for(i=0;i<n;i++)
{
cin>>line[i];
if(line[i]>=0)
have+=line[i];
else
{
if(have>=abs(line[i]))
have+=line[i];
else
kid++;
}
}
cout<<have<<" "<<kid<<endl;
}

The main problem I can see in your code is this:
int line[n];
This is known as a VLA (Variable Length Array) and it is not supported in C++. It is valid in C. Most compilers still allow this behaviour due to the fact that C++ is based on C, but it is not valid C++ code. In a previous question, I found out that clang supports designated initializers, when gcc and vc++ did not. The reason is because some compilers like clang, support c99-extensions by default. My point is that just because the code compiles, it doesn't mean it's always right.
If you compile with the -pedantic argument, you will see that the compiler is warning you about this being a C99 feature. Have a look at the rextester example here. From the comments below, using -pedantic-errors in the compiler flags, will prompt an error.
If you know the size of the array before run-time, then you should use a static array int line[4];, but if you don't then you need to use a dynamic array. std::vector is essentially a dynamic array that also handles memory for you. It's easy to use and very efficient. std::vector<int> line;
You can read more about the vector container here: http://www.cplusplus.com/reference/vector/vector/
Btw, I tried your code in rextester, ideone and repl.it and I got the same results: 22 1. I think what you are witnessing it undefined behaviour.
Also, you can qualify int n with constexpr and it'll be fine.
constexr int n = 200;
int line[n]; //now it's ok.
But this again means that you know the size of the array at compile time.

Related

Is there is any way that i can define user input as my string array length? [duplicate]

I am trying to create this simple program which displays the data of the array back to the user again.. I want to create a variable length array. In this program the user is first asked the number of elements of the array followed by the data.
The problem is that in some of the IDE this code runs completely fine but in others it gives the error that variable length array is not allowed.... So what is correct?
void main()
{
int t;
cin>>t;
int ar[t];
for(int i=0;i<t;i++)
{
cin>>ar[i];
}
for(int i=0;i<t;i++)
{
cout<<ar[i]<<"\t";
}
}
For eg. This doesn't work in Turbo C++... But runs in this IDE's
http://www.tutorialspoint.com/compile_cpp11_online.php
https://www.codechef.com/ide
Standard C++ does not support variable length arrays. Some implementations provide it as an extension but as you have already found out, relying on them makes for non-portable code.
I recommend you use a std::vector instead. It works with pure standard C++.
int size;
if (!(std::cin >> size) || (size < 0))
throw std::invalid_argument {"bad size"};
std::vector<int> numbers (size);
In GCC and Clang, you can use the -pedantic compiler switch to turn off any non-standard extensions. This will help you avoid accidentally writing non-portable code. Of course, you should also compile with -Wall, -Wextra and -Werror.

Visual Studio Community 2019 requires a constant value inside square brackets [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 years ago.
I've been trying to find answers for this lately, but I just can't seem to understand why the compilers for C++ that Microsoft has been using can't compile such a code :
#include<iostream>
int main()
{
int n;
std::cin >> n;
int x[n];
}
It gives those errors :
However, this code compiles on a lot of different compilers.
Could someone point me to somewhere, I couldn't find any answers for this.
See How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)? for discussion on the matter. Briefly, C++ requires that raw arrays have a constexpr for their size, so they it knows how much memory to allocate for it at the moment it's declared instead of at runtime after receiving user input. Some compilers I suppose are more permissive on this.
Variable length arrays are not standard C++. Some compilers provide them as a language extension but they are not portable.
This code shouldn't compile.
In C++, arrays are statically allocated and their required memory must be specified at compile time.
Hence, a user input is not a suitable value for array declaration. If you want a dynamic array, you can try using malloc like this:
#include<iostream>
int main()
{
int n;
std::cin >> n;
int* x = (int*)malloc(n*sizeof(int));
}
This way, the array is actually stored in heap, and can have any arbitrary size, changing over lifetime of the program.
Another alternative is std::vector of course.

c++ weird array by user input [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 4 years ago.
As I know in c++ if you want to crete an array, you must give constant value for its size. But here:
int main(){
int a;
cin >> a;
int b[a] = {};
for (int i = 0; i<a ; i++){
b[i] = a;
cout << b[i];
}
return 0;
}
If i input 5
output:
55555
It works fine in a way i can't understand in dev c++. If i run this in visual studio 2017, it gives error. Can anyone explanin why?
Are you using GCC by any chance? This is a GCC extension and it's enable by default. In fact it's quite a dangerous one because it's fairly easy to cause a stack overflow on your program. It's roughly the same as using alloca().
In order to disable it, you should use a compiler flag called -Wpedantic. This will make your compiler issue a warning. (see this demonstration)
ISO C++ forbids variable length array ‘b’ [-Werror=vla]
As I know in c++ if you want to crete an array, you must give constant value for its size.
Correct. If you use a non-constant value, then the program is ill-formed. Yes, the program that you show is ill-formed.
It works fine in a way i can't understand ... Can anyone explanin why?
C++ compiler may allow compilation of an ill-formed program. This enables the compilers to extend the language. It appears that you were using a non-standard extension to C++.
This is what the GCC compiler says about your program:
warning: ISO C++ forbids variable length array 'b' [-Wvla]
int b[a] = {};
^

swapping first and last element in stack c++ using stl

i created an account so i can get some help with stacks in STL , i need to write a function that takes stack as a parameter and swaps the first element with the last element , i searched the site for some help i found one :"https://stackoverflow.com/a/36188943/9990214" , i tried the same thing , but i keep getting this error : expression must have a constant value with red line under "int tmp[sz-1];".
it keeps giving me the error before reaching the main , any help would be appreciated , keep in mind am trying to write the function using STL.
ps : i tried replying with a comment to the person who answered the question but it's not allowing me to do that because i need 50 reputation.
using namespace std;
void rev(stack<int>&x){
int sz=x.size(),mytop,mybottom;
mytop=x.top();
x.pop();
int tmp[sz-1],i=0;
while(!x.empty()){
mybottom=x.top();
tmp[i++]=mybottom;
x.pop();
}
stack<int> returnIt;
returnIt.push(mybottom);
for(i=0;i<=sz-3;i++){
returnIt.push(tmp[i]);
}
returnIt.push(mytop);
while(!returnIt.empty()){
int tt=returnIt.top();
x.push(tt);
returnIt.pop();
}
}
The reason you're getting an error is that variable-length arrays are not a part of standard C++. This matters for your definition of tmp:
int tmp[sz-1], i=0; //sz is not known at compile-time, therefore, this is invalid code
Some compilers will allow code like this by allowing VLA's, but not being standard, you should use a different solution. Usually, for tasks like this, std::vector is ideal:
std::vector<int> tmp(sz - 1);
int i = 0;
This should compile (so long as you #include<vector> alongside your other includes), and should have the behavior you expect from your code.

C++ graphics initgraph error (syntax or missing-file flaw?)

[SOLVED]
I'm using CodeBlocks (C++) on Win10. I tried to create a simple graphic program - I downloaded a certain graphics package (graphics.h and other two files related to BGI - I added the lib in settings etc.). I tried this program but there seems to be a problem at the line marked. I ported this from Pascal (as my teacher -yes, it's about college-, only shows us Pascal programs) in a correct manner I suppose, or at least partially. The problem is certainly with the pointers (EDIT AFTER SOLVING: it wasn't!, check my answer). I'll give more details if needed.
The Question: Where I did mistakes and how to correct them (what to write, what to add, what to delete) so that it wil work fine? If there is no mistake in the code, but I NEED some files for graphics to work on my compiler, which are the files, where to get them and where to put them?
ERROR (not quite, now's a "warning"):
deprecated conversion from string constant to 'char*' [-Wwrite-strings]
(see code a bit below)
Please read: I would BE VERY GRATEFUL for a fixed version of my program. Refferences are USELESS, so unless you REALLY want to help me (thank you!), leave this page. My only related capacity is to compare two versions of a program that are intended to do the same thing but one has a mistake (or more).
CODE (updated!):
#include<iostream>
#include<graphics.h>
#include<conio.h>
using namespace std;
int main(){
int gr,xmax,ymax,r;
int gm,gd;
gd=DETECT;
gm=0;
initgraph(&gd, gm, "C:\\TC\\BGI"); /*edit(solved): followed the tutorial linked in my answer; not a directory in my PC.*/
gr=graphresult();
if(gr!=grOk) cout<<"Error!";
else {xmax=getmaxx();
ymax=getmaxy();
cout<<"Resol.: "<<xmax+1<<"x"<<ymax+1;}
setcolor(7);rectangle(0,0,xmax,ymax);setcolor(5);line(0,0,xmax,ymax);line(0,ymax,xmax,0);setcolor(3);
for(r=(ymax+1)/2;r>=0;r--) circle((xmax+1)/2,(ymax+1)/2,r);
getch();
closegraph();
return 0;
}
graphics.h source and guide: http://www.codewithc.com/how-to-include-graphics-h-in-codeblocks/
You get an error because you try to take the address of something that isn't stored in memory.
Character literals like ' ' are basically translated into integers by the compiler, and you would not expect e.g. &32 (the ASCII code for space) to work would you?
If you're supposed to pass a string, use double-quotes and no address-of operator, as in " ".
Just read your edited question and want to mention about LValue:
"LValue" means something which has an address i.e. you may apply the address operator &. LValue because it may appear on Left side of an assignment.
A variable has an address and thus is an LValue.
A character constant (e.g. 'A') has actually type int and is not an LValue.
A string constant (e.g. "Hello") has type const char[] (compatible to const char*) is (may be surprisingly) an LValue because the compiler must allocate data storage for it somewhere.
Thus, even the following would "work":
#include <stdio.h>
int main()
{
char *sz = "Hello";
printf("address of \"%s\": %p\n", sz, sz);
printf("address of sz: %p\n", &sz);
sz[0] = 'h';
printf(sz);
return 0;
}
I compiled and tested with gcc and cygwin (on Windows 10 - 64 bit):
$ gcc -o test-cstringlvalue test-cstringlvalue.c
$ ./test-cstringlvalue.exe
address of "Hello": 0x403060
address of sz: 0x61cc6c
Segmentation fault (core dumped)
So you see two important things:
It compiles fine. (Changing an LValue is not suspected by the compiler.)
It crashs because a constant string has been modified.
(It probably will not crash on any platform.)
However, I recommend not to modify string constants but it demonstrates what I tried to explain about LValues.
I hope the sample does not confuse you any more. I warmly recommend to get a good C book. There are a lot of tricks possible in C but the language itself is tricky.
Edit:
Reading my own text again I felt a little bit uncertain if I told it exactly right: Thus I googled again and found this link: SO: Why are string literals l-value while all other literals are r-value?.
I fixed the obvious syntax errors and marked the places by comment `/* <-- FIXED */:
#include<iostream>
#include<graphics.h>
#include<conio.h>
using namespace std;
int main()
{
int gr,xmax,ymax,r;
int gm,gd; /* <-- FIXED */
gd=DETECT;
gm=0;
initgraph(&gd, &gm, "C:/Users/MyName/Desktop/WORKFOLDER/"); /* <-- FIXED */
gr=graphresult();
if(gr!=grOk) cout<<"Error!";
else {
xmax=getmaxx();
ymax=getmaxy();
cout<<"Resol.: "<<xmax+1<<"x"<<ymax+1;
}
setcolor(7);rectangle(0,0,xmax,ymax);setcolor(5);line(0,0,xmax,ymax);line(0,ymax,xmax,0);setcolor(3);
for(r=(ymax+1)/2;r=0;r--) circle((xmax+1)/2,(ymax+1)/2,r);
getch();
closegraph();
return 0;
}
Unfortunately, I cannot compile and test it. (I don't have BGI libs and don't want to install it.)
Solved!
Downgraded CB (v16 to v13 I think, not sure if it was necessary) and replaced some files.
I followed this tutorial:
https://m.youtube.com/watch?v=FxCdMM9H66I
It was quite the same as the link in the question but probably (one or more of) those files (graphics.h, winbgim.h, libbgi.a) from the previous link were bugged, had some unobvious errors or so, or were incompatible etcetera), while the files given by the maker of the vid I provided in this answer were corrected. I don't (really want to) know.
Now, the warning still appears, BUT the program runs perfectly!
I replaced r=0 with r>=0 , this was a minor mistake. (I'll update the question if I still can)
Thanks for... your help! (Can't say "nothing", I'd be mean)
:)
EDIT:
Some "cleaner" version that shows the circle (just realised the variant in the question doesn't display the filled circle):
#include<iostream>
#include<graphics.h>
#include<dos.h>
using namespace std;
main(){
int gm,gd,gr,xmax,ymax,r;
gd=DETECT;
gm=0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
gr=graphresult();
if(gr!=grOk) cout<<"Error!";
else {xmax=getmaxx();
ymax=getmaxy();
cout<<"Res.: "<<xmax+1<<"x"<<ymax+1;}
setcolor(7);
rectangle(0,0,xmax,ymax);
setcolor(5);
line(0,0,xmax,ymax);
line(0,ymax,xmax,0);
setcolor(3);
for(r=(ymax+1)/2;r>=0;r--) circle((xmax+1)/2,(ymax+1)/2,r);
getch();
closegraph();
}
EDIT2:
Downgrading not necessary CONFIRMED! :D
The warning should go away if you replace the literal with an array that can be converted to char*:
char driver[] = "";
initgraph(&gd, &gm, driver);