9 Replies - 1002 Views - Last Post: 03 April 2013 - 10:22 PM Rate Topic: -----

#1 babyhands  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Problem with user inputting into string array

Posted 02 April 2013 - 05:24 PM

Hey guys, so I have a little question for you today! Basically I have to write a program that takes in ISBN numbers from the user in the form of a string array, and check if it is valid. The user can input as many ISBN's as they want, so this means they can use up multiple slots in the array, and when the user enters CTRL+D (which is 4 on the ascii table) that is when the program knows they are done inputting ISBN's so the computer can spit out if the ISBN's are correct or not. So a mini sample input would be:
0-89237-010-6
0-8306-3637-4
0-06-017758-6
This_is_garbage

and the output would be:
0-89237-010-6 is correct.
0-8306-3637-4 is correct.
0-06-017758-6 is correct.
This_is_garbage is incorrect.




Now this is where I have my question. I can't ask the user how many ISBN's they are going to input, so I can't use that as the size for the array. I also can't have an array with unused memory (so like they only put in 5 ISBN's into an array initialized with 20 slots). So some how I need to initialize an array, and start looping through it letting the user input whatever they like, and then end inputting when the user types CTRL-D. This is what I have so far:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	
	string isbn[20];
	int i = 0;
	while (isbn[i] != 4){
		cin >> isbn[i];
		i++;
	}
}



I am getting an error in the line while (isbn[i] != 4) because it says I am trying to compare a string to an int, but i know CTRL-D is 4 so i though it would read it as 4? Also, I initialized the array to size 20, but that is just because I can't initialize it without giving it a size, but of course I don't always want it to be size 20. Sorry if this is super long, I have been googling my heart out trying to find a solution to no avail, so I decided to type this up so you guys would know exactly what my problem is!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with user inputting into string array

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem with user inputting into string array

Posted 02 April 2013 - 06:44 PM

In Unix Ctrl-D sends an End-of-transmission character, in windows Ctrl-Z is used.

Are you using Unix?

The Ctrl-D should fail the stream.


The first character of a string, in the string array is isbn[ i ][0], but you probably don't want to use that.
Was This Post Helpful? 0
  • +
  • -

#3 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Problem with user inputting into string array

Posted 02 April 2013 - 08:01 PM

can you use a do-while loop and ask the user after each input if they want to add another ISBN number?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Problem with user inputting into string array

Posted 02 April 2013 - 08:16 PM

Based on the constraints you have above, and based on you using the std::string class, then presumably, you can use the std::vector class as well.

So in pseudo code, you would have:
vector<string> tempArray;
while (cin >> s)
    push s into tempArray;

string * finalArray = new string[tempArray.count()];
foreach item in tempArray
    put item into finalArray.



Why you would want to use a string array when a vector<string> is so much better, one only wonders?

This post has been edited by Skydiver: 02 April 2013 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 babyhands  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Problem with user inputting into string array

Posted 02 April 2013 - 08:55 PM

View Post#define, on 02 April 2013 - 06:44 PM, said:

In Unix Ctrl-D sends an End-of-transmission character, in windows Ctrl-Z is used.

Are you using Unix?

The Ctrl-D should fail the stream.


The first character of a string, in the string array is isbn[ i ][0], but you probably don't want to use that.
No I am not on Unix, I am on a windows. I remember him saying something about Ctrl-D on Unix exiting, and thats why we are using it. So are you saying i shouldn't use != 4 since i am on a windows?

View PostMidi_, on 02 April 2013 - 08:01 PM, said:

can you use a do-while loop and ask the user after each input if they want to add another ISBN number?

Well it depends on what you mean, if you mean after each ISBN entered i have something like cout << "Do you want to enter another ISBN?";, if so, then no we can't do that. They just enter all the ISBN's, we store them in a string array, then cout which ones are valid and which ones arent.

View PostSkydiver, on 02 April 2013 - 08:16 PM, said:

Based on the constraints you have above, and based on you using the std::string class, then presumably, you can use the std::vector class as well.

So in pseudo code, you would have:
vector<string> tempArray;
while (cin >> s)
    push s into tempArray;

string * finalArray = new string[tempArray.count()];
foreach item in tempArray
    put item into finalArray.



Why you would want to use a string array when a vector<string> is so much better, one only wonders?
Because we have not learned about vectors, and I am sure he only wants us using what we have learned, so either string arrays or character double arrays (which I am not exactly sure how to use)
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem with user inputting into string array

Posted 02 April 2013 - 09:20 PM

It is common for these key combinations to be used to end input, it also allows redirection from files.

If you use these keys then the stream will be set to eof, and this will exit the loop.

while (cin >> isbn[i]){



Character = 4, I have shown you how to access the first character of a string if you want to try and see if that will work.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Problem with user inputting into string array

Posted 02 April 2013 - 09:34 PM

If you can't use the std::vector, one approach could be like this:
count = 0;
allocated = 0;
while (cin >> s)
{
    if count == allocated
    {
        if allocated == 0
            newSize = 1
        else
            newSize = allocated * 2

        newArray = string[newSize]
        for i = 0..allocated-1
            newArray[i] = array[i]

        array = newArray
    }

    put s into array[count]
    count = count + 1
}


Was This Post Helpful? 0
  • +
  • -

#8 babyhands  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Problem with user inputting into string array

Posted 03 April 2013 - 06:58 PM

Hey guys, sorry I have not responded in a while, been busy with school etc. So I talked to my professor and he said initializing the array to 20 actually is fine, so now I just how to figure out how to drop out of the loop with ctrl+d, this is the code I have so far:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	
	string isbn[20];
	int i = 0;
	while (isbn[i][0] != 4){
		cin >> isbn[i];
		i++;
	}


cout << "your loop works yay!!!"; 
}



I know it has barely changed, but i am just trying to figure out how to get out of the loop now. I have also tried != '^D' because i know that that is what ctrl D looks like when entered into the console. Also, I am getting an error with my cout statement saying it is ambiguous?

thanks! I really appreciate the help guys
Was This Post Helpful? 0
  • +
  • -

#9 babyhands  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Problem with user inputting into string array

Posted 03 April 2013 - 07:31 PM

View Postbabyhands, on 03 April 2013 - 06:58 PM, said:

Hey guys, sorry I have not responded in a while, been busy with school etc. So I talked to my professor and he said initializing the array to 20 actually is fine, so now I just how to figure out how to drop out of the loop with ctrl+d, this is the code I have so far:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	
	string isbn[20];
	int i = 0;
	while (isbn[i][0] != 4){
		cin >> isbn[i];
		i++;
	}


cout << "your loop works yay!!!"; 
}



I know it has barely changed, but i am just trying to figure out how to get out of the loop now. I have also tried != '^D' because i know that that is what ctrl D looks like when entered into the console. Also, I am getting an error with my cout statement saying it is ambiguous?

thanks! I really appreciate the help guys


So I figured out it doesn't work because I increment i before I check, so the while is being checked for an empty string, so this is how i got it to work:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	
	string isbn[20];
	int i = 0;
	do{
       i++;
		cin >> isbn[i];
      } 
	while (isbn[i][0] != 4);


cout << "your loop works yay!!!" << endl; 
}



Now my problem is, won't my first index of the array always be empty because i is incremented first? How can I do this while using the first index of the string array?
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem with user inputting into string array

Posted 03 April 2013 - 10:22 PM

You increment up to 1, so you could increment up to 0 instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1