arrays

so im making a program that will help me practice making arrays. i thought it was a really rockin code (at least for me) until i tried to debug and i got a few errors. if you could point out any issues you catch that would be great. ive read and reread it a few times and havent come up with anything. heres my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

// Testing.cpp :playing with volatile memory is safer than playing with fire
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int rem [7]

int main ()
{
	int f;
	short r=0;
	
	cout << "please enter number to be remembered";		// input into the array
	cin >> f;
	
	if(r<7 && r >=0)
		while(f!=0){
			int rem [r]=f;
			cout << "you have put "<< f << " in [" << r <<"] of the array.\n\nyou have "<< 6-r <<" more open space(s) in the array.";
			r++;
			cin << f;
		}
	else
		cout << "array is full.";



	return 0;
}


after this section i plan to write up a loop telling the computer to recite the chosen element. straight forward. i hope.
You forgot to include the errors you got. I'm lazy and I don't want to read what a compiler already read and assessed.
int rem [r]=f; Your are re-initializing the array you want to store in everytime the loop runs. Remove int from said line and you should be fine:)
Last edited on
ahhhh right thanks fafner.
ill re-debug and then ill include any more errors i may have @webJose. sorry bout that
First off, as fafner said, you're re-initializing the array in line 21.
Also, your cin has the wrong operators in line 24, finally, you forgot a semicolon in line 9.

I don't understand why you include stdafix.h but the following compiles for me without error:

http://tinypaste.com/20c834

I'd recommend that before you actually start working with arrays that you start working with more basic stuff. Generally you're not going to want to have any global variables in a program (that means don't define things like int x = 2 unless it's in a function such as int main() or void MyFunctionName() etc...)
You should also try and avoid #include -ing things you don't need. It's actually quite useful knowing what they do.

You should also look at your output, your output will look very messy when you run the program and at the end of it, you don't even see what your array is full of so the exercise is moot.
Last edited on
ok so i fixed what fafner said and fixed a semicolon i forgot after creating int rem [7]; but i am still getting a whole bunch of these errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 1
1>  Testing.cpp
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\documents and settings\tom\my documents\visual studio 2010\projects\testing\testing\testing.cpp(23): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'std::istream'
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(968) : see declaration of 'std::operator <<'

etc. etc.
Your cin's >> are facing the wrong way. (Line 24)
Last edited on
@throstur The reason he is using stdafx.h is because he is using visual studio. If you do not choose empty project you have to add stdafx.h
Last edited on
I hope you don't mind, but I tweaked your program a little. Added the output for the 'rem' array, and made the inputting to be a for(..;..;..) loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Testing Arrays.cpp : playing with volatile memory is safer than playing with fire

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int rem[7];
string numbers[] = {"zero","one","two","three","four","five","six"};

int main (int)
{
	int f;
	int r;
	
	cout << "Please enter a number to be remembered  : ";		// input into the array

	for (r = 0; r < 7; r++)
	{
		cin >> f;
		rem[r]=f;
		cout << "You have " << numbers[6-r] <<" open space";
		if (6-r != 1)
			cout << "s";
		cout << " left in the array.";
		if ( r!=6)
			cout << " Next number please.. ";
	}
	cout << endl << "The array is now full. The array holds the following..." << endl;
	for (f=0;f<7;f++)
		cout << "Rem[" << numbers[f] <<"] = " << rem[f] << endl;
	
	return 0;
}
Last edited on
ha! jeese that was dumb. thanks whitenite1 i was looking forward to making that part for myself but ill check it against yours and see what comes back. thanks!
@f1ddl3r

Sorry if I messed up your fun. Maybe for your next practice array program, you could shuffle the array, like a deck of cards. That should prove entertaining for awhile.. ;)

no problemo! i saw what you were doing so i never read the code and therefore no fun was ruined. here is my completed code. it doesnt work beautifully but it works just as well as i want it to for now. for some reason it continues to accept input past r = 7 so if any of you notice why itd be great to know but everything else works great. here it is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Testing.cpp :playing with volatile memory is safer than playing with fire
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int rem [7];

int main ()
{
	int j;
	int f;
	short r=0;
	
	cout << "please enter number to be remembered (hit 0 to exit)\n\n";		// input into the array
	cin >> f;
	
	if(r<7 && r >=0)
		while(f!=0){
			rem [r]=f;
			cout << "you have put "<< f << " in [" << r <<"] of the array.\n\n\t\tyou have "<< 6-r <<" more open space(s) in the array.\n\n";
			r++;
			cin >> f;
		}
	else 
		cout << "array is full.";
	
	if (f==0){
		cout << "\n\nyou have exited.\n\n enter element in array to be retreived. (hit 7 to exit)\n";
	}

		cin >> r;

		while (r>=0 && r <=6){
			cout << rem [r];
			break;
		}

		

	int wait;
	cin >> wait;
	return 0;
}


thanks everyone!

Topic archived. No new replies allowed.