How can I let the user enter how many strings he/she wants to input

i want the user to input how many variables he/she will need so there

Page 1 of 1

6 Replies - 1374 Views - Last Post: 26 June 2008 - 11:34 AM Rate Topic: -----

#1 izzyprime86  Icon User is offline

  • New D.I.C Head

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

How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 08:14 AM

Hello Dream.in.code nation,

I would like to know if there is some kind of formula that will tell the script how many varialbes the user will need.

Ex: "Input amount of strings you will use"
cin>> 8

And then the user would input 8 strings. And then the 8 strings would be used in the scripts

In the overall prespective I'm trying to input strings of hexadecimal ranges like 112a-112f or just strings of hexadecimal numbers like a3b1 and then if its a range like "112a-112f" I would break that string into two strings like "112a" and "112f" then I would sstream those two strings into integers and would print all the hexadecimal number in between.

Problem:

well as you can see I been having to insert a variable for each single string hex number or for each string hex range. I need to be able to input 200 ranges or so and I need to find a way that the user can tell the script how many ranges(strings) he will input and then have those strings be converted into integers and so on like i have been able to do(see code).


#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];char hex2[10];char hex3[10];char hex4[10];char hex5[10];char hex6[10];char hex7[10];
	char hex8[10];char hex9[10];char hex10[10];char hex11[10];char hex12[10];char hex13[10];char hex14[10];
	char hex15[10];char hex16[10];char hex17[10];char hex18[10];char hex19[10];char hex20[10];char hex21[10];
	char hex22[10];char hex23[10];char hex24[10];char hex25[10];char hex26[10];char hex27[10];char hex28[10];
	char hex29[10];char hex30[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;
}



thanks in advance,

Izzy

This post has been edited by izzyprime86: 26 June 2008 - 11:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I let the user enter how many strings he/she wants to input

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 08:25 AM

you can use dynamic memory allocation for that.

say user needs n number of integers. So you will ask to user for that n.

after it just have a pointer which will point to the memory block of n integers which you will allocate dynamically.




cout<<"how many variables";
cin>>n;

int *i;
i=new int[n];




I always refer this site first when I get any problem in C or C++. I would suggest you to do the same.
Was This Post Helpful? 0
  • +
  • -

#3 izzyprime86  Icon User is offline

  • New D.I.C Head

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

Re: How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 09:17 AM

ok that makes sense now could I do that with strings, say that the user wants to input like a 100 strings would that work as well or will it only work with ints?
Was This Post Helpful? 0
  • +
  • -

#4 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 09:23 AM

you can create an array of strings using dynamic memory allocation. example:
#include <string>
#include <iostream>
using namespace std;

int main()
{
   int numstrings;
   cout << "How many strings? ";
   cin >> numstrings;
   string * array = new string[numstrings];
}



That code will give you an array of strings with a number of elements equal to the user's input.
Was This Post Helpful? 0
  • +
  • -

#5 izzyprime86  Icon User is offline

  • New D.I.C Head

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

Re: How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 09:40 AM

View Postpolymath, on 26 Jun, 2008 - 09:23 AM, said:

you can create an array of strings using dynamic memory allocation. example:
#include <string>
#include <iostream>
using namespace std;

int main()
{
   int numstrings;
   cout << "How many strings? ";
   cin >> numstrings;
   string * array = new string[numstrings];
}



That code will give you an array of strings with a number of elements equal to the user's input.



Thanks polymouth but please excuse my primative question, will give me an x amount of strings when I input x number or will it give me a string with an x amount of charcters inside that string?



Maybe I should be more clear in what I'm looking for. (thanks for your patience everyone) i would like to enter a number (ex: 4) and when I input 4 it will create 4 strings with a buffer size of 10 and then I can input what I want in each string.


Thanks again everyone,

Izzy
Was This Post Helpful? 0
  • +
  • -

#6 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 10:08 AM

Example of output from previous code:

How many strings: 5
[WINDOW CLOSE]

string * array can now be used as if it was string array[5].

Oh, and at the end of your program you need to add delete[] array; so that you don't have memory leaks.

to put it all together:

#include <string>
#include <iostream>
using namespace std;

int main()
{
   int numstrings;
   string dummy;
   cout << "How many strings? ";
   cin >> numstrings;
   getline(cin, dummy); //grab newline to prevent bugs
   string * array = new string[numstrings];
   for(int control=0;control<numstrings;control++) {
      cout << "Input string " << (control+1) << ": ";
      getline(cin, array[control);
   }
   cout << "You typed: \n";
   for (int control=0;control < numstrings;control++) {
      cout << "String " << (control + 1) << ": " << array[control] << endl;
   }
   cout << "Press any key then enter to exit. ";
   cin >> aray[0];
   delete[] array;
   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 izzyprime86  Icon User is offline

  • New D.I.C Head

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

Re: How can I let the user enter how many strings he/she wants to input

Posted 26 June 2008 - 11:34 AM

I have updated my original post please take a look at it because i have made my question alot clearer.

Thank you,

Izzy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1