C++ working with Integers

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1773 Views - Last Post: 14 October 2007 - 04:40 PM Rate Topic: -----

#1 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

C++ working with Integers

Posted 09 October 2007 - 09:21 PM

I am pretty new at this. I have the basics down, but I can't figure out how to complete this... Read 10 integer numbers from the file 'Project_1_Input.dat', print the numbers
on a single line of output in the order they were read from the file, then print the
same numbers in reverse order on a new line.
The program should expect to find the data file in the default directory.

I think that I am approaching this incorrectly, I can't even get it to compile.

#include <cstdlib>
#include <iostream>

using namespace std;

int main( )

/*  */

{

   int Project_1_Input = "10" "9" "8" "7" "6" "5" "4" "3" "2" "1"/n 
  "10"9""8""7" "6" "5" "4" "3" "2" "1"
   cout >> Project_1_Input;




	system("PAUSE");
	return EXIT_SUCCESS;
}



Any suggestions are appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: C++ working with Integers

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ working with Integers

Posted 09 October 2007 - 09:46 PM

well there are a couple of problems with the code, and what it appears to be trying to do doesn't quite add up with what you are saying you want it to do.

you declare a variable Project_1_Input that you appear to be trying to store a bunch of numbers in. however, an int can only hold a single value - you need to declare it as an array.

if you're trying to output the entire array, you need to display each element of the array individually. using cout with the variable name will output the address of the array. and to use cout, you need to use the stream insertion operator <<, not the extraction operator >>.

the following code is similar to what you have, but a bit cleaned up. it declares an array of 10 ints and initializes them to the first 10 integers, and then outputs them to the screen. it doesn't have any of the file input that you mentioned, because you don't have any of that in your code and it's probably best to just get this down for now.
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {

	int Project_1_Input[] = {0,1,2,3,4,5,6,7,8,9};   //initialization of array of ints to contain integers 0-9
	
	for (int i=0; i<10; ++i) {
		cout << Project_1_Input[i] << " ";   //run through each element and output
	}

	//system("PAUSE");
	return EXIT_SUCCESS;
}


hope that gives you a starting point. feel free to post again if you have any questions or any further problems.

-jjh

(edit) p.s. please use code tags when posting code, or highlight your code and click the # button

This post has been edited by jjhaag: 09 October 2007 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

Re: C++ working with Integers

Posted 10 October 2007 - 03:50 AM

Ahhh... Thanks alot!


So when attempting to store a sequence in a single file, I have to (In the simplest terms) create an array to basically make my large sequence of numbers print from the file?

Thanks again for the assistance (greatly appreciated), I just want to make sure that I have the logic of "when to use it" down.
Was This Post Helpful? 0
  • +
  • -

#4 Smarf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: C++ working with Integers

Posted 10 October 2007 - 05:49 AM

View PostJaMilt, on 10 Oct, 2007 - 03:50 AM, said:

Ahhh... Thanks alot!


So when attempting to store a sequence in a single file, I have to (In the simplest terms) create an array to basically make my large sequence of numbers print from the file?

Thanks again for the assistance (greatly appreciated), I just want to make sure that I have the logic of "when to use it" down.



Your code initializes the variable, not the file. I'm assuming you're doing a section on sequential access files. Basically you can have the program read through a completely seperate text file and pull the information from it. You can also write to that same file in various ways (appending to the end, erasing everything completely etc).

I'm a beginner as well and don't know the syntax off the top of my head but basically you want:

- The statement that reads the txt or dat file.
- Store each number from that file in an array element using a loop
- Display those elements in reverse order with another loop
Was This Post Helpful? 0
  • +
  • -

#5 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ working with Integers

Posted 10 October 2007 - 01:05 PM

you didn't do any file i/o in your initial code, so i just cleaned up what you had to make it work as best as i understood.

when you're dealing with files, it is certainly possible to read the file and display it to the screen, without having to allocate an array to store the file information. you do need to deal with file streams, though. the following code gives an example of how to do that:
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
	
	//	declares a file stream object
	fstream infile;
	
	//	opens a new stream, associated with your file, with the io stream flag 
	//	mode set to input (ios::in) to allow reading
	infile.open("Project_1_Input.dat", ios::in);
	
	//	test if file opened successfully
	if (!infile) {
		cout << "Error opening file for reading.  Program will now exit." << endl;
		return EXIT_SUCCESS;
	}

	//	declares a variable for storing a single number from the file
	int Project_1_Input; 

	//	while the current position in the file is not the end of the file ( eof() ),
	//	perform a stream extraction from the file into the variable, then output 
	//	it to the screen
	while (!infile.eof()) {
		infile >> Project_1_Input;			//	get number from file
		cout << Project_1_Input << " ";		//	output to screen
	}
	cout << endl;
	
	//	close the file to make everyone happy
	infile.close();

	system("PAUSE");
	return EXIT_SUCCESS;
}


if you have any questions let me know. hope that helps.

-jjh

This post has been edited by jjhaag: 10 October 2007 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

Re: C++ working with Integers

Posted 10 October 2007 - 03:24 PM

Thanks for the additional info, quick question based on that last bit.

I've been playing around with your code, and I attempted to reverse the sequence of numbers. I used:



#include <cstdlib>
#include <iostream>


using namespace std;  

int main()
{
	
int Project_1_Input[] = {1,2,3,4,5,6,7,8,9,10}; 

for (int i=10; i > 1; --i) { 
		cout << Project_1_Input[i] << " "; 



  system("PAUSE");
	return EXIT_SUCCESS;


I get 10-1, but i also get a odd numeral before hand, as if it multiplies the numbers first. Any idea where my weakness lies? I've tried turning the "--i" into +, -, and ++, but I get a loop that often crashes DOS.



Thanks alot again for the help, I'm just trying to get it all to really "click" in what's left of my mind :P.

This post has been edited by born2c0de: 13 October 2007 - 12:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ working with Integers

Posted 10 October 2007 - 11:15 PM

this is a common problem when starting out working with arrays. the trouble lies in the fact that the positions in an array of n elements are referred to by the indices 0...n-1.

in the first cleaned up code that i posted, notice how the for loop is constructed:
for (int i=0; i<10; ++i) {
		cout << Project_1_Input[i] << " ";   //run through each element and output
}

in the first run of the loop, it outputs Project_1_Input[0]. in the next, Project_1_Input[1]. the last output to occur is Project_1_Input[9], since when i==10, the test (stop) condition in the for loop has been met, so it skips doing the loop and moves on to the remaining code.

in your example, you start with i=10. so, you are accessing Project_1_Input[10], which in fact does not exist. what you are seeing is a some "garbage" data that lies in memory after the block allocated for your array. this happens because an array name is basically just the address of the first element of the array. when you use the subscript operator Project_1_Input[i], you are specifying the element at an offset of i elements from the first position. thus, in this case, you need to start the output at i=9.

i think that if you run your program again and check carefully, you will also find that the output stops terminates with 3 - the numbers 2 and 1 don't get outputted. this is because your test condition in the for loop reads i>1. so the loop will terminate execution after i=2, since the test condition is false for i=1. thus, the last element you output is Project_1_Input[2], which in this case has the value 3. in this case, you should be using i>=0. that way the loop continues until the element at position 0 is outputted.

accessing the garbage data at the end of an array is a dangerous thing to do. in the very best case, it returns garbage that you don't do anything with. after that, you can get into some bad stuff. you might, for instance, use that garbage to calculate a bunch of other things later in the program; when you get wonky output at the end, it makes troubleshooting very hard. you can also crash your program (as you found), other programs, or in rare instances when the data you are messing with is going to go back into a system file, mess up or disable your operating system. so be careful about array bounds, and remember: the last element of an array is found in position number_of_elements-1, and the first is found in position 0.

hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#8 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

Re: C++ working with Integers

Posted 11 October 2007 - 03:56 AM

It helps a great deal, thanks!

This post has been edited by JaMilt: 11 October 2007 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 Smarf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: C++ working with Integers

Posted 11 October 2007 - 07:43 AM

This is for my own benefit.

int Project_1_Input[] = {1,2,3,4,5,6,7,8,9,10}; 


Don't you need to specify the size of the array when declaring it or does the compiler do it for you since you're initializing it with a set of numbers?

Like this?

int Project_1_Input[10] = {1,2,3,4,5,6,7,8,9,10};

Was This Post Helpful? 0
  • +
  • -

#10 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ working with Integers

Posted 11 October 2007 - 12:10 PM

for 1D arrays, you can just write the values, comma-separated, within the braces, and the compiler figures it out. for higher dimensional arrays, you need to include bounds for all of the dimensions except the first one:
int temp[][3]={ {1,2,3}, {1,2,3} };

if you try to do it without the boundaries, your compiler will yell at you, something like this (specific msg depends on compiler, of course):
int temp[][]={ {1,2,3}, {1,2,3} };

Quote

main.cpp:9: error: declaration of `temp' as multidimensional array must have bounds for all dimensions except the first


-jjh
Was This Post Helpful? 0
  • +
  • -

#11 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

Re: C++ working with Integers

Posted 12 October 2007 - 08:45 PM

Does C++ have a reverse command? I've tried multiple combinations of - and +, and though I can print 20 digits I cannot reverse the stream (attempting to print 1-10, then 10-1) while still keeping the same file name. My most recent (failure) attempt:

int Project_1_Input[] = {1,2,3,4,5,6,7,8,9,10 "\n" 10,9,8,7,6,5,4,3,2,1}; //initialization of array of ints to contain integers 0-9

for (int i=0; i<20; ++i) {
cout << Project_1_Input[i] << " ";


"\n" and endl; both didn't work for me, those are the only separation commands that I am aware of.
Was This Post Helpful? 0
  • +
  • -

#12 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ working with Integers

Posted 12 October 2007 - 09:37 PM

just do two different loops, don't change the size or contents of your array. you've already got code that prints it out forwards, and other code that prints it out in reverse, so just combine them using two separate for loops.

the '\n' and endl work in output statements, not in the declaration of arrays.

one other problem - "while still keeping the same file name" - you aren't dealing with files. this is still just work with arrays; you don't have any file input/output steps in your code.

-jjh
Was This Post Helpful? 0
  • +
  • -

#13 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

Re: C++ working with Integers

Posted 13 October 2007 - 05:09 AM

Excellent idea, thanks!

Thanks to C++, my at-work productivity is in the crapper :P

This post has been edited by JaMilt: 13 October 2007 - 05:10 AM

Was This Post Helpful? 0
  • +
  • -

#14 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

Re: C++ working with Integers

Posted 14 October 2007 - 03:26 PM

Another question that I have along the same lines:

Is it possible to reverse the array by using a command such as the one I have added below:



int Project_1_Input[] = {0,1,2,3,4,5,6,7,8,9}; //initialization of array of ints to contain integers 0-9

for (int i=0; i<10; ++i) {
cout << Project_1_Input[i] << " ";


void reverseArrayElements(Project_1_Input[], int 9, int 0);

}


What i am trying to do with this is find alternate methods to print the previously created array print in reverse order (using the same "Project_1_Input").


Thanks!
Was This Post Helpful? 0
  • +
  • -

#15 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ working with Integers

Posted 14 October 2007 - 03:41 PM

yeah, it's possible, but you'd have to write the actual function to do so. do you mean to actually reverse the order of the elements in the array itself, and then output the array in order? or do you want to just have another way of running through the array backwards and outputting each element as you go?

-jjh
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2