7 Replies - 481 Views - Last Post: 17 October 2012 - 09:28 PM Rate Topic: -----

#1 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

While Loop/Switch function Error

Posted 17 October 2012 - 02:30 PM

The error I get is that sometimes my while loop/switch program will not work right. I am trying to convert mnemonic numbers to numeric numbers, but it will only convert the first letter put in or if you type in ADGJMPTW it will print out the phone numbers that match, but if you put ABDJMPTW it will only read the first number and thats it. SOmetimes with the while loop it will either print one thing or nothing at all. Can I recieve any advice/assistance?
#include<iostream>
#include<string>
using namespace std;

int main()
{
	char aNum;
	char dNum;
	char gNum;
	char jNum;
	char mNum;
	char pNum;
	char tNum;
	char wNum;
	char duv;

	cout << "Welcome to the mnemonic number to numeric number converter" << endl << endl;
	cout << "This program allows you to convert nmeonic numbers to numeric numbers" << endl << endl;

	cout << "Enter your letters that you want to convert" << endl << endl;
	cin >> aNum >> dNum >> gNum >> jNum >> mNum >> pNum >> tNum >> wNum;

	while((aNum != '#') || (dNum != '#') (gNum != '#') || (jNum != '#') || (mNum != '#') || (pNum != '#') || (tNum != '#') || (wNum != '#'))
	{
		switch((aNum) || (dNum) || (gNum) || (jNum) || (mNum) || (pNum) || (tNum) ||(wNum)) 
		{
		if((aNum == 'A') || (aNum == 'B') || (aNum == 'C'))
				cout << 2;
		if((dNum == 'D') || (dNum == 'E') || (dNum == 'F'))
				cout << 3;
		if((gNum == 'G') || (gNum == 'H') || (gNum == 'I'))
				cout << 4;
		if((jNum == 'J') || (jNum == 'K') || (jNum == 'L'))
				cout << 5;
		if((mNum == 'M') || (mNum == 'N') || (mNum == 'O'))
				cout << 6;
		if((pNum == 'P') || (pNum == 'Q') || (pNum == 'R') || (pNum == 'S'))
				cout << 7;
        if((tNum == 'T') || (tNum == 'U') || (tNum == 'V'))
				cout << 8;
        if((wNum == 'W') || (wNum == 'X') || (wNum == 'Y') || (wNum == 'Z'))
				cout << 9;
		default: 
				cout << "Invalid Input";
		}
	}
			
				cin >> duv;

		
			
			return 0;


}



Also I am just a beginner so I am trying to stay away from using the do function and other things that are way past if statements. The if/else statements and a brief explanation of the while function is as far as I've learned.

This post has been edited by jimblumberg: 17 October 2012 - 03:03 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: While Loop/Switch function Error

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: While Loop/Switch function Error

Posted 17 October 2012 - 02:35 PM

What are you expecting this statement to do?
switch((aNum) || (dNum) || (gNum) || (jNum) || (mNum) || (pNum) || (tNum) ||(wNum))


Was This Post Helpful? 0
  • +
  • -

#3 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

Re: While Loop/Switch function Error

Posted 17 October 2012 - 02:39 PM

View Postjjl, on 17 October 2012 - 02:35 PM, said:

What are you expecting this statement to do?
switch((aNum) || (dNum) || (gNum) || (jNum) || (mNum) || (pNum) || (tNum) ||(wNum))


I'm quite new at this so please excuse my ignorance on code functions, but I thought the switch function literally meant it wouls switch between the values that each one of those char's equal: aNum==A, B, || C dNum==D, E, || F etc?
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: While Loop/Switch function Error

Posted 17 October 2012 - 03:07 PM

A switch takes a single variable and "switches" code execution based on the case which the variable falls under.

example
int x = 3;

switch(x) // takes single variable
{
   case 0: // case for when x == 0
      std::cout<<"zero\n";
      break; // prevent from falling into the other cases below
   case 1: // case for when x == 1
      std::cout<<"one\n";
      break;
   default: // all other cases
      std::cout<<"Error\n";
      //no break since there are no more cases to fall into
}


This post has been edited by jjl: 17 October 2012 - 03:16 PM

Was This Post Helpful? 2
  • +
  • -

#5 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

Re: While Loop/Switch function Error

Posted 17 October 2012 - 03:24 PM

View Postjjl, on 17 October 2012 - 03:07 PM, said:

A switch takes a single variable and "switches" code execution based on the case which the variable falls under.

example
int x = 3;

switch(x) // takes single variable
{
   case 0: // case for when x == 0
      std::cout<<"zero\n";
      break; // prevent from falling into the other cases below
   case 1: // case for when x == 1
      std::cout<<"one\n";
      break;
   default: // all other cases
      std::cout<<"Error\n";
      //no break since there are no more cases to fall into
}


This reply was helpful, but I am trying to get the while loop (right?) to read more than just the first letter. I get constant errors or It only reads the first letter in each if statement. ADGJMPTW==23456789, but if I want to enter a mnemonic phone number like CALL SAM it only prints out the phone number that corresponds with the first letter. My while loop fails every time
Was This Post Helpful? 0
  • +
  • -

#6 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

Re: While Loop/Switch function Error

Posted 17 October 2012 - 07:16 PM

vfgb
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: While Loop/Switch function Error

Posted 17 October 2012 - 09:02 PM

You could use a string and the [] operator to access each character.

getline

string::operator[]
Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: While Loop/Switch function Error

Posted 17 October 2012 - 09:28 PM

If you are familiar with arrays, I would suggest you use them instead of using individual variables.
You could try something like this.

char mnemonic[10];
int mnem_count;
cout<<"Enter number of mnemonics";
cin>>mnem_count;
cout<<"Enter the mnemonic numbers that you would like to convert";
for(int i=0;i<mnem_count;i++)
{
  cin>>mnemonic[i];//This will save you a great deal of variable initialization if you notice
}



Do something on the above lines and see.You may also have to change your if statements accordingly.

I didn't quite understand what you intended to do in the while loop.
Apart from that, please take into consideration what jjl has said and I am sure you will get the program right.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1