Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone!

How can I make this code work (see below)

Thanks ! :-)

What I have tried:

#include <iostream>
using namespace std;

int main()
{

int n, numbers;

cin >> n;

cin >> numbers[n]

}
Posted
Updated 1-Jan-19 6:02am

Start by getting hold of good book on C++. What you have written makes no real sense.
 
Share this answer
 
Two things:
1) We can't tell you, as we have no idea what it was supposed to do! So we have no idea what exactly it "working" should be like!
2) There are two stages to "getting code working": the first is getting rid of compiler errors; the second is getting rid of runtime errors.
For the first, you need to look closely at what the output from your compiler is telling you - it normally gives you the line (or at least the line number) and a message. In this case, it is probably complaining about array subscripts and invalid types, and that's because numbers is not an array, and you can't use an array subscript on a "standard integer". You can fix this by allocating a number of integers to numbers. For example:
C++
int n, number[10];
will allocate 10 integers, so you can then use the array syntax:
cin >> numbers[n]
Will work a little better. It still won't compile cleanly, because you forgot to add the terminating semicolon to the line:
cin >> numbers[n];
will do better. But allocating just ten integers probably isn't going to be a good idea...

Now you are down to runtime errors: for that, you need a clean compile and to use the debugger - a quick google will find you loads of tutorials in using the Visual Studio debugger so check a few of them, and give it a try!
 
Share this answer
 
Quote:
How can I make this code work

How one can answer this question as we don't know what the code is supposed to do?

If n is not 0, the code is doing some buffer overflow because you are using numbers like an array.
Advice: find a tutorial and follow it to kearn the basics.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
Look back at the answer I gave you previously. You have not allocated memory for the numbers and you don't have a loop acquiring the numbers. Without those things it will be impossible for this code to work.
 
Share this answer
 
you really need to learn the basic of C++ to understand what you want to do.

cook book:
- ask for n
- allocate buffer for numbers
- ask n times for input (a loop)
- do what you want
- delete the number buffers

please learn the language ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900