Help with c++ problem (array)

was trying to solve a random c++ problem

Page 1 of 1

4 Replies - 623 Views - Last Post: 04 November 2008 - 07:17 AM Rate Topic: -----

#1 Pat234  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Help with c++ problem (array)

Post icon  Posted 02 November 2008 - 04:29 PM

i was trying to tackle a random homework assignment on this forum (link: http://www.dreaminco...topic19360.htm)
and came up with this(look below at code) but unfortunately there's an error (not a compiler one) where if the user inputs more than 7 characters, the next character is used for the 'answer' variable near the end of the program
any ideas?

btw, i wrote comments in the code saying where I THINK the problem may be

#include <iostream>
using namespace std;

int main()
{
	char num[7] = {}; //this will store the characters the user inputs
	char answer = 'y'; //for while loop

	while(answer != 'n')
	{
	 cout << "Please enter the text for a phone number: "<< endl;
	 cin >> num[0] >> num[1] >> num[2] >> num[3] >> 
			num[4] >> num[5] >>  num[6]; // But messes up if you enter more than 7 characters, need help here :-/
	  cout << endl << "You typed: " << num << endl;
	 for(int x = 0; x <8;x++)
	 //cout <<num[x];
	 {
	  if(x == 3)
	  cout << "-";	 
		   
	  if(num[x] == 'a'||
		 num[x] == 'A'||
		 num[x] == 'b'||
		 num[x] == 'B'||
		 num[x] == 'c'||
		 num[x] == 'C')
		 cout << "2";
	  if(num[x] == 'd'||
		 num[x] == 'D'||
		 num[x] == 'e'||
		 num[x] == 'E'||
		 num[x] == 'f'||
		 num[x] == 'F')
		 cout << "3";
	  if(num[x] == 'g'||
		 num[x] == 'G'||
		 num[x] == 'h'||
		 num[x] == 'H'||
		 num[x] == 'i'||
		 num[x] == 'I')
		 cout << "4";
	  if(num[x] == 'j'||
		 num[x] == 'J'||
		 num[x] == 'k'||
		 num[x] == 'K'||
		 num[x] == 'l'||
		 num[x] == 'L')
		 cout << "5";
	  if(num[x] == 'm'||
		 num[x] == 'M'||
		 num[x] == 'n'||
		 num[x] == 'N'||
		 num[x] == 'o'||
		 num[x] == 'O')
		 cout << "6";
	  if(num[x] == 'p'||
		 num[x] == 'P'||
		 num[x] == 'q'||
		 num[x] == 'Q'||
		 num[x] == 'r'||
		 num[x] == 'R'||
		 num[x] == 's'||
		 num[x] == 'S')
		 cout << "7";
	  if(num[x] == 't'||
		 num[x] == 'T'||
		 num[x] == 'u'||
		 num[x] == 'U'||
		 num[x] == 'v'||
		 num[x] == 'V')
		 cout << "8";
	  if(num[x] == 'w'||
		 num[x] == 'W'||
		 num[x] == 'x'||
		 num[x] == 'X'||
		 num[x] == 'y'||
		 num[x] == 'Y'||
		 num[x] == 'z'||
		 num[x] == 'Z')
		 cout << "9";
	 }
	 
	 cout<< endl <<endl << "Do you want another number(y/n)?" << endl; //if over seven characters the 8th character gets used here
	 cin >> answer;
	 
	}

	return 0;
}



This post has been edited by Pat234: 02 November 2008 - 04:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with c++ problem (array)

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Help with c++ problem (array)

Posted 02 November 2008 - 08:25 PM

Just put a cin.ignore() in there...where I put it. That will make it ignore anything left in the buffer, and your program will continue as it should.
:)

	  if(num[x] == 'w'||
		 num[x] == 'W'||
		 num[x] == 'x'||
		 num[x] == 'X'||
		 num[x] == 'y'||
		 num[x] == 'Y'||
		 num[x] == 'z'||
		 num[x] == 'Z')
		 cout << "9";
	 }
	 cin.ignore(numeric_limits<streamsize>::max(),'\n');//ignore all characters left in the buffer
	 cout<< endl <<endl << "Do you want another number(y/n)?" << endl; //if over seven characters the 8th character gets used here
	 cin >> answer;
	 
	}

	return 0;
}



This post has been edited by OliveOyl3471: 02 November 2008 - 08:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 Pat234  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Help with c++ problem (array)

Posted 03 November 2008 - 08:45 PM

cool thanks OliveOyl3471,
didn't know those cin.ignore was even important (including those other cin DOT commands)
thanks again :)
Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Help with c++ problem (array)

Posted 03 November 2008 - 09:58 PM

You are very welcome. I'm glad I could help you. That's what this site is here for. In fact, if it wasn't for someone from this site helping me, I would not have even known so I could tell you about it.

:D
Was This Post Helpful? 0
  • +
  • -

#5 D1gitalIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-October 08

Re: Help with c++ problem (array)

Posted 04 November 2008 - 07:17 AM

Yes, cin.ignore() can be used. What is happening is that your array is reading from the input "stream", when you enter more characters than what can be held by your array, those "leftovers" are still stuck in the input stream. The next call to cin will pull the first leftover piece inside of the stream to be put in whatever you point the insertion operator (>>) at.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1