Make a constant amount of arrays and user inputs what he needs

Creating as many char strings that the user needs and then having each

Page 1 of 1

1 Replies - 470 Views - Last Post: 30 June 2008 - 12:41 PM Rate Topic: -----

#1 izzyprime86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-June 08

Make a constant amount of arrays and user inputs what he needs

Posted 30 June 2008 - 10:26 AM

Hello Community,

I have developed this code that you can input a string and then it will take that string and turn it into two strings and the two strings will be seperated at a hyphen Ex: "abcd-1234" splits into string 1 = "abcd" string 2 = "1234".

Well the code below displays how to do this function when one string is entered by the user. If I want more strings to be read a time I would need to create more varialbls.

So i was wondering if there was a way where I can creat some type of array that I can allow the user to enter 500 strings but if the user only enters 20 then the script will only do the functions to those twenty.

I hope there is a way to do this because the only solution that I have come up with at the moment is to make variables for each string that would be entered.

thanks in advance,


Izzy


  #include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){ 

	cout<<"Please Enter a Maximum of 30 Values\n\n";
	cout<<"If you have less than 30 values just apply a zero after you last number with a space between them before pressing enter\n\n";
	cout<<"When done placing values press Enter\n\n";

	
	time_t curr=time(0);
	ofstream res("result.txt", ios::app);
	res<< "Time Processed: " << ctime(&curr) <<"\n";

	int zzz;						  //exits
	char hex1[10];				//30 values
	////////////////////111111111
	cin>>hex1;

	if(strlen(hex1)==1){						// if one of the inputs of the string was zero
		goto stop;
						}
	else{
	if(strlen(hex1)< 9){					//strlen returns the length of the char array 1234-1234
	  
	  cout<<hex1<<"\n";
	  res<<hex1<<"\n";
	} 
	
	else{ 
	  
	  char *aa  = strtok(hex1,"-");		   //places in a group (group f) the characters from begging up to the hypen
	  char *bb = strtok(NULL, "\0"); //places in a group (group b) the character from after the hypen to the end of the string
	  int aaa;
	  int bbb;
	  stringstream con1(aa);
	  stringstream con2(bb);
	  con1>>hex>>aaa;
	  con2>>hex>>bbb;

	  while(aaa<=bbb){
		  cout << hex << setw(4) << setfill('0') << aaa << endl;  // prints zeros
		  res << hex << setw(4) << setfill('0') << aaa << endl;  // prints zeros
		  aaa++;
	  }
	  
	}


  stop: 
  cout << "\n\nYour luns have been proccessed\n\n";
  cout << "Please enter q and find resluts on file result.txt\n\n";
  cin>>zzz;

}  
return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Make a constant amount of arrays and user inputs what he needs

#2 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Make a constant amount of arrays and user inputs what he needs

Posted 30 June 2008 - 12:41 PM

Yes, you can do that. What you want is dynamic memory allocation.

#include <iostream>

int main(void)
{
  int i;
  int *array;

  std::cout << "Enter the number of ints to create\n";
  std::cin >> i;

	// Allocate a array of integers that contains the requested amount
  array = new int[i];

	// Just prints out garabe
  for(--i; i >= 0; --i)
	std::cout << array[i] << "\n";

	// Cleanup the memory, since we are done with it
  delete[] array;
  return 0;
}


If you don't want to do the memory management yourself, you can use something like a vector.

#include <iostream>
#include <vector>
#include <stdlib.h> // rand()

int main(void)
{
  int i;
  std::vector<int> array;

  std::cout << "Enter the number of ints to create\n";
  std::cin >> i;

  for(--i; i >= 0; --i)
  {
	  // Add a new interger to our array
	array.push_back(rand());
	  // Print out the new value
	std::cout << array.back() << "\n";
  }

  // The vector will clean up it's memory automatically
  return 0;
}


You can also just use a string. This is probably going to be the easiest rout for you, since it can be used to read in a entire line of input

#include <iostream>
#include <string>

int main(void)
{
  std::string str;

  std::cout << "Enter a string\n";
  getline(std::cin, str); // Read in everything they enter

  std::cout << "You entered: |" << str << "|\n";

  return 0;
}


If you use getline() with a string, it will read in everything the user enters, up to the carriage return/new line. If you use cin >> str;, it will only fill str with everything up to the next new line or the next space.

If you use getline(), you can then use a loop with strtok() to separate each word with your delimiters set to " -". IE, a space or a hyphen. The only catch to this, is that you'll have to manually duplicate the content of the string into a char array, which should be manually allocated at run time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1