noob here looking for cin >> char or int input help!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1259 Views - Last Post: 09 April 2012 - 06:17 PM Rate Topic: -----

#1 helmania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-April 12

noob here looking for cin >> char or int input help!

Posted 02 April 2012 - 10:37 PM

Hello everyone,

Newbie here reporting for duty(help). I'm writing a Binary Search program and I'm stuck in how to get an input from command line to either send it up to the array if it's a number or quit the program if it's an 'x'. So far I've only been taking one type of input only. It's either a int or char, but how do I take an input if it can be either one of them at any giving time. I played around with taking the input as a char and convert it to an int if it's not an x, but because it's a char, cin will only take the very first character. So 34, will only register as 3 and then 4 as if it's two inputs. Plus the conversion doesn't work in my favor either. I'm just stuck here and any help would be greatly appreciated.

thanks


my code is below. My problems start from the "while" statement.
---------------------------------------------

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

int Binsearch(int data[], int numElements, int searchKey)//this is the function which does the binary search
{
	int search;
	int total = numElements/2;

	for(int i=(numElements/2); i < numElements; i = i)
	{
		if (searchKey == data[i])
		{	search = i;
			break;
		}

		cout << "i = "<< i;

		if (searchKey > data[i])
			i = i + (total*.6);	

		if (searchKey < data[i])
			i = i - (total*.6) ;

		total = total*.6;

		if (total < 1)
			total = 2;
	}

	return search;

}
	
int main()
{
	int searchKey;
	int dbase[21] = {1, 4, 5, 6, 9, 14, 21, 23, 31, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};

	cout << "List = {";  //list all the numbers for the viewer to see
	for (int i = 0; i<17; i++)
	{
		cout << dbase[i];
		if (i < 16)
			cout <<", ";//thi adds a comma after every number except the last one. 
	}
	cout <<"}" << endl;


	bool quit = false;
	while (!quit)
	{
		unsigned char inputChar = 58;
	//	cout << (int)inputChar;
		int c = 0;
		int searchKey = 0;
		cout <<"Enter a search Key (or 'x' to exit): ";
		cin  >> inputChar >> searchKey;

	if (inputChar == 'x' || inputChar =='X')
		{	
			cout <<"Exiting..."<< endl;
			quit = true;
		}
	else 
		{	
			int c = int(inputChar);
			cout << inputChar;
		//	cout << Binsearch(dbase, (int) inputChar, searchKey);
		
		}

	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: noob here looking for cin >> char or int input help!

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: noob here looking for cin >> char or int input help!

Posted 02 April 2012 - 10:50 PM

It's very awkward to be asking for both an "exit character" and a search key at the same time -- forcing a user to enter a search key when they really just want to exit.

I'd suggest that you prompt for the search key (an int) at the beginning of the loop, and then just before the end of the loop prompt for the character (a char) that will indicate whether to remain in the loop or quit. If they elect to remain in the loop, that will bring them back to the beginning where they will be prompted for a new search key.

This post has been edited by r.stiltskin: 02 April 2012 - 10:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 helmania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-April 12

Re: noob here looking for cin >> char or int input help!

Posted 02 April 2012 - 11:25 PM

View Postr.stiltskin, on 02 April 2012 - 10:50 PM, said:

It's very awkward to be asking for both an "exit character" and a search key at the same time -- forcing a user to enter a search key when they really just want to exit.

I'd suggest that you prompt for the search key (an int) at the beginning of the loop, and then just before the end of the loop prompt for the character (a char) that will indicate whether to remain in the loop or quit. If they elect to remain in the loop, that will bring them back to the beginning where they will be prompted for a new search key.


Thank you for your response. But as you can see in the Instruction to the user, they can either enter a search key or 'x' to exit from the very beginning. That is how I have to do it. Surely there has to be a way!

thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 helmania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-April 12

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 03:50 PM

so none of the great programmers here can help me with my problem???

Is it that difficult?
Was This Post Helpful? 0
  • +
  • -

#5 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:06 PM

Okay, your problem is that when using the (int) cast, it doesn't get the number the character is. So if the character is 4, the int isn't going to equal 4. It will equal somewhere in the 50s because of the fact that when you cast a char to an int, it returns the ASCII value of the character. Also, since InputChar is only supposed to tell when to exit(right?), I would use getch() as it allows the user to enter in a single character without waiting for enter.
Was This Post Helpful? 0
  • +
  • -

#6 helmania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-April 12

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:27 PM

View Postsuperkb10, on 04 April 2012 - 04:06 PM, said:

Okay, your problem is that when using the (int) cast, it doesn't get the number the character is. So if the character is 4, the int isn't going to equal 4. It will equal somewhere in the 50s because of the fact that when you cast a char to an int, it returns the ASCII value of the character. Also, since InputChar is only supposed to tell when to exit(right?), I would use getch() as it allows the user to enter in a single character without waiting for enter.


thank you for your reply. I have not come across getch() yet in my studies, so any way you could show me a tiny example within my program code of how I would use it to solve my dilemma?

thanks a bunch!
Was This Post Helpful? 0
  • +
  • -

#7 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:35 PM

It's used pretty much like any other function, here:


InputChar = getch();

if(InputChar == 'x' || InputChar == 'X')
{
    exit(1);
}




The exit function just tells the program to stop. Have fun!

This post has been edited by superkb10: 04 April 2012 - 04:37 PM

Was This Post Helpful? -1
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:41 PM

What operating system are you using? The getch() function is a non-standard DOS/Windows function and should be avoided for most usages.

If you want to accept either character or numeric entry then you must use a std::string or C-string for your user input and then validate the entry to determine if it is a number or a character string. If you are just trying to determine if the string contains a certain character then using a std::string to hold the user input and then using std::string.find() to determine if the string contains your character. If it does not then you could convert the string to a number, using stringstreams or if your compiler supports the new C++11 standard you could use stox to convert the string to a number.

Jim

View Postsuperkb10, on 04 April 2012 - 06:35 PM, said:

It's used pretty much like any other function, here:


InputChar = getch();

if(InputChar == 'x' || InputChar == 'X')
{
    exit(1);
}




The exit function just tells the program to stop. Have fun!


The exit() function should be avoided in most cases in C++ programs. This C function does not call C++ destructors properly and can lead to data loss and other problems. Also getch() is a non-standard operating system specific function that should also be avoided.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:43 PM

Windows 7, it works perfectly fine when I run it. I've always used this, but then again, I'm not expert at C++. You should just need to include conio.h
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:49 PM

I don't have a conio.h header file on my computer. As I said that is a operating system specific function that should be avoided. Plus it will not solve the OP's original problem. The getch() function will only accept one character, the OP wants to accept a number or a string.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:51 PM

Oh, I completely misunderstood the question then. I assumed he was looking for the user to first enter in a char to determine whether or not they wanted to continue or not. Also, jimblumberg, are you using Visual Studio? I believe conio.h is included with Visual Studio Express.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:53 PM

Visual Stdio will not run on my computer. As I said that is an operating system specific header file.

Jim

This post has been edited by jimblumberg: 04 April 2012 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#13 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: noob here looking for cin >> char or int input help!

Posted 04 April 2012 - 04:59 PM

Ah, okay, then nevermind about my suggestion. I don't know that much about C++, I read a bit of "C++ for Dummies" just for a little insight into it, but I mainly program in C# and in "C++ for Dummies" it uses the getch() and I found exit() as a way to prematurely end a program.
Was This Post Helpful? 0
  • +
  • -

#14 helmania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-April 12

Re: noob here looking for cin >> char or int input help!

Posted 09 April 2012 - 04:16 PM

Thank you all for all your replies. After losing a lot of sleep and being driven to sheer frustration. I ended up using atoi to change the input from string(I could not achieve it with char) to int if it's not x. That worked very well and now I'm beyond this. I also did some optimization of my code to make it more efficient... lol.

I place my final codes here to the benefit of any noobs like me who may run into similar problems.


/* this program searches the index of an array for a searchKey (data[i]) input by the user. It starts from the middle and eliminates
every half where the searchKey is less or greater than. Thus the search can happen much faster and efficiently as opposed to running 
through every element in the array. */

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

int Binsearch(int data[], int numElements, int searchKey)//this is the function which does the binary search
{
	int search;//the varialble I use to for the actual searching of index
	int total = numElements/2;// total keeps track of the number of elements divided by half each pass. 
	int count = 0;
	for(int i=(numElements/2); i < numElements; i = i)//i = i from each pass since we are  not incrementing each pass but moving i by half at every pass
	{

		if (searchKey == data[i])//if the very first number from the index happens to be the seachKey number, break out of the loop and return the number
		{	
			search = i; //equals in index the number is found on
			break;
		}
	//	cout << "index = "<< i << " number = " << data[i] <<endl; //keeps track for my own purpose

		if (searchKey < data[i]) //if seachkey number is less than where we are in the middle...
		{
			i = i - (total*.5); //make i equal to i - half of the remaniing half. So that would move us to the left of the search line
		}

		if (searchKey > data[i]) //if the seachkey number is greater than where we are in the middle...
		{	
			i = i + (total*.5);//make i equal to i + half of the remaining half, moving us to the right of the search line. I use .6 instead of .5 so that the answer will always be rounded higher when the decimal is dropped. 
		}

		total = total*.5; //since in every pass number of elements (total) drop by half, I have to cut total by half in order to keep the number of elements left accurate.	
	
	//	cout << "total = " << total << endl; //keeps track for my own record
		
		if (total < 2)
		{
			total = 2;
			count++;
		}

		if (count > 3)//if total is raised to "2" more than three times, that means the number isn't in the array. So I picked 999 as the "invalid" method
		{	
			return 999;
			break;
		}
	}

	return search;

}
	
int main()
{
	int searchKey;
	int dbase[21] = {1, 4, 5, 6, 9, 14, 21, 23, 31, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};

	cout << "List = {";  //list all the numbers for the viewer to see
	for (int i = 0; i<21; i++)
	{
		cout << dbase[i];
		if (i < 20)
			cout <<", ";//thi adds a comma after every number except the last one. 
	}
	cout <<"}" << endl;

	bool quit = false;
	while (!quit)
	{
		string inputChar;
	
		cout <<"Enter a search Key (or 'x' to exit): ";
		cin  >> inputChar;

	if (inputChar == "x" || inputChar =="X")//exits the program if user types x
		{	
			cout <<"Exiting..."<< endl;
			quit = true;
			break;
		}
	 	
			int searchKey = atoi(inputChar.c_str()); //line converts inputChar from string to int type
			int index = Binsearch(dbase, 21, searchKey); //make call to function
			
			if (index == 999)
			{
				cout << searchKey << " not found!"<<endl; 
			}
			else
				cout << searchKey <<" is in the position "<< index <<endl;	

			cout << endl;
	}
}


Was This Post Helpful? 0
  • +
  • -

#15 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: noob here looking for cin >> char or int input help!

Posted 09 April 2012 - 05:38 PM

I'm just going to give a small suggestion while trying to avoid making further mistakes, but I believe you could have made this a lot easier if first, you simply asked if they wanted to stop, and then used cin>> on an integer. Then you wouldn't really need to convert from a string to an integer, but I suppose it's good practice.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2