Simple code

closed account (zqMDizwU)
Can someone come up with a simple code that will add one everytime I press "press any key to continue"?

I have made an array that will spit out a list of numbers but I am trying to make it just output a number each time to my array. Here is my code for an array btw:

#include <iostream>

using namespace std;


int j;

int myArr[1];

int main(){
for (int j = 0; j < 10; j++){

myArr[j] = j + 1;
cout <<myArr[j] << " ";
}
system("pause");
}
we arent going to do your homework for you. try doing it yourself
closed account (zqMDizwU)
It's not hw. It is a concept that I am trying to utilize for my hw.
Last edited on
pressing a key and entering a key is different. If you want when they actually press you are going to have to use a non-standard method. As far as the counter that is pretty straight forward something like:
1
2
3
4
5
unsigned counter = 0;
while(gettingInput)
{
    if(keyboardIsHit) ++counter;
}
I'm not sure what your trying to do, so I'll just go by what your code is saying.
1.) You are only making two element in it. you've got the right idea for the for loop, but like I said before, there's only 2 elements so writing j < 10 isn't necessary.

2.) The loop goes through because the system pause is outside the loop. If it were to do as you said, it would look something like this:
1
2
3
4
//previous stuff
system ("PAUSE");
myArr[j] = j + 1;
cout << ...


Please USE CODE TAGS next time.
P.S. use
 
system ("PAUSE>nul")

to get rid of the words displayed and put a cout before it to make your own words fro when the program pauses.

http://answers.yahoo.com/question/index?qid=20101017212427AAhOvdk
expanding on what gilbit said, kbhit() can be used.

You are only making two element in it.

im pretty sure it only has one element in it

P.S. use





system ("PAUSE>nul")

pps: dont use system at all. and if you do PAUSE>nul then you will create a file called null in the PWD.
Last edited on
closed account (zqMDizwU)
Thank you so muck for that jazpen.

What I am trying to do is get an array that spans from your desire, for argument sake 10.

Instead of the code giving me 1 2 3 4 5 6 7 8 9 10

I'm trying to make a loop that goes
1
Press any key to continue . . . //press key
2
Press any key to coninue . . .// and so on, up to 10 in my example.

This is not hw hence it is simple code. I am trying to apply it to something a lot more complex. I am a novice.
Little Bobby Tables wrote:
and if you do PAUSE>nul then you will create a file called null in the PWD.
Oh really? Try it and see for yourself. :) (On Windows, that is.)
closed account (zqMDizwU)
huh when I use pause>nul I get 1 2 3 4 5 6 7 8 9 10

but I wouldn't use that to reach my objective.
Little Bobby Tales, there are two elements in an array that is:
 
int array1[1]

It starts with [0] which is the first element and [1] is the second element.

demondiamond, do you want the array to have no values before the loop of outputting the values is or do you want the values preset before the cout loop activates?
closed account (zqMDizwU)
preset and when I int arr[0] it gives me an error saying array must be greater than zero, but that has nothing to do with this or me. Present not like cin >> input; type of thing. Just a loop that outputs an element of the array every time u press enter.
Last edited on
1
2
3
4
5
6
7
int array1[100];
for (int var1 = 0; var1 < 100 ; var1++)
{
array1[var1] = var1 + 1;
cout << array1[var1] <<"\n" ;
system ("PAUSE") ; //or nul wahtever
}

This will work

closed account (zqMDizwU)
OMG this is so cool "Thank You" jazpen!!
No problem, but next time use code tags.
"" to start and "" to close

Oops.
[code] and the same thing but a forward slash in front of code to end.
closed account (zqMDizwU)
Oh okay like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int array1[100];

int main(){
for (int var1 = 0; var1 < 100 ; var1++)
{
array1[var1] = var1 + 1;
cout << array1[var1] <<"\n" ;
system ("PAUSE") ; //or nul wahtever
}
}
I guess lol
You can change the 100 to wahtever you want but remeber you need to have
 
#inlcude <cstdlib> 

to get system to work.
closed account (zqMDizwU)
Ok it worked regardless, but I will always add it, because I'd rather be safe than sorry, especially when programs get way more complicated. Here's the end result, but yeah I could put a million if I liked. Thank you jazpen. And when it came down to it, it seemed like a miss place of swirly brackets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>

using namespace std;

int myArr[10];

int main(){
	
for (int j = 0; j < 10; j++)
{
	myArr[j] = j + 1;
	cout <<myArr[j] << "\n"; //or cout <<myArr[j] << " ";
	
	system("pause"); //or system("pause>nul"); 
	}
}
@ldm: is nul a special windows keyword then? regardless you still shouldnt use system pause.

Little Bobby Tales, there are two elements in an array that is:

int array1[1]


It starts with [0] which is the first element and [1] is the second element.

no... thats wrong. that creates an array of one element. if you want an array of two elements, then you can do int array[2] and then access like this: array1[0] /*first element*/; array1[1] /*second element*/

also creating an array of 100 elements isnt good. i would use one of the stl containers
Topic archived. No new replies allowed.