5 Replies - 450 Views - Last Post: 28 April 2009 - 04:54 PM Rate Topic: -----

#1 lalaithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 09

Having trouble with if statement

Posted 28 April 2009 - 04:17 PM

I'm having trouble with the if statement working correctly in this program...it continues on with no invalid input error message even if I input the wrong letter in the M or K field.

 //get units for size of message
	cout << endl << "Enter units for size of message (K or M): ";
	cin >> unitFile;
	if ( unitFile == 'K' || 'k' )
	{
		//units are K - compute file size
		numberBytes = size * KILO;
	}
	else if ( unitFile == 'M' || 'm')
	{
		//units are M - compute file size
		numberBytes = size * MEGA;
	}
	
	else
	{   //invalid input
		cout << "Invalid units for size of message - must be M or K" << endl;
		cout << endl << "Enter units for size of message (K or M): ";
		cin >> unitFile;
	}



Full program:
 //Course: 4002-208-70
//Author: lalaithan
//Project 4 in 20083
//Note: This idea used in labs for 20071
//Name:  208project2.cpp
//Purpose: Program to calculate the number of seconds to transmit a message
//		 that is transmitted in IP packets 0f 1500 bytes with 28 bytes
//		 of overhead.
//		 The size of the message must be > 0 and < 1024
//		 The program allows the user to specify the file size in
//		 KB, MB, or GB.
//		 The user must specify one of the following transmission speeds:
//		 19,200  56,000  or 1,544,000 bps.

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

int main()
{
	//constant declaratin section
	const int KILO = 1024;   //value of kilo
	const int MEGA = KILO * KILO;  //value of mega
	const int DATA_BYTES = 2340; //maximum number of data bytes in a packet
	const int OVERHEAD_BYTES = 45;   //overhead bytes in a packet
	const int PACKET_LENGTH  = DATA_BYTES + OVERHEAD_BYTES;
									 //maximum number of bytes in a packet
									 //including the overhead
	//speeds for each option
	const int SPEED_OPTION1 =	19200;
	const int SPEED_OPTION2 =	56000;
	const int SPEED_OPTION3 =   256000;
	const int SPEED_OPTION4 =  1544000;
	const int SPEED_OPTION5 = 54000000;
							
	//variable declaratin section
	int size			  = 0;	//length of message in KB or MB - lengthOfMessage
	int numberBytes	   = 0;	//number of bytes in message
	int numberBits		= 0;	//number of bits in messag
	int bitsPerSec		= 0;	//transmission speed in bps
	double timeMsg		= 0.0;  //time to transmit message - no packets
	int numFullPackets	= 0;	//number of full packets
	int bytesLastPacket   = 0;	//message bytes in last packet
	double timeFull	   = 0.0;  //time per full packet
	double timeLast	   = 0.0;  //time last partial packet
	double timeTransmit   = 0.0;  //time for full and last partial packet
	double packetOverhead = 0.0;	   //% for packet overhead
	char unitFile		 = ' ';	//units of file M or K  
	int choice			= 0;  //choice for transmission speed"
	int timeMin		   = 0;  //time in minutes if time to transmit is over 60
	double timeSec		= 0; //time is left over seconds after minutes removed
		 
	//get valid size of message
	cout << "Enter size of message to be transmitted: ";
	cin >> size;
	cout << endl;
	
	/**
	if (size <= 1)
	{   //print error message
		cout << "Invalid size of message - must be > 1" << endl;
		system("pause");
		return 0;
	}**/
	
	//error message that allows user to retry
	while ( !(size >= 1)   )
	{
		cout << "Invalid size of message - must be > 1"
			 << endl;
		cout << "Enter size of message to be transmitted: ";
		cin >> size;   
	}
	
	
	//get units for size of message
	cout << endl << "Enter units for size of message (K or M): ";
	cin >> unitFile;
	if ( unitFile == 'K' || 'k' )
	{
		//units are K - compute file size
		numberBytes = size * KILO;
	}
	else if ( unitFile == 'M' || 'm')
	{
		//units are M - compute file size
		numberBytes = size * MEGA;
	}
	
	else
	{   //invalid input
		cout << "Invalid units for size of message - must be M or K" << endl;
		cout << endl << "Enter units for size of message (K or M): ";
		cin >> unitFile;
	}
	

	//print menu of options and get users choice
	cout << endl;
	cout << "Available transmission speeds" << endl;
	cout << "1." << setw(8) << SPEED_OPTION1 << " bps" << endl;
	cout << "2." << setw(8) << SPEED_OPTION2 << " bps" << endl;
	cout << "3." << setw(8) << SPEED_OPTION3 << " bps" << endl;
	cout << "4." << setw(8) << SPEED_OPTION4 << " bps" << endl;
	cout << "5." << setw(8) << SPEED_OPTION5 << " bps" << endl;
	cout << "Enter choice: ";
	cin >> choice;
	
	//set transmission speed based on choice 
	//print error message if choice is invalid
	if (choice == 1)
	{
		bitsPerSec = SPEED_OPTION1;
	}
	else if (choice == 2)
	{
		bitsPerSec = SPEED_OPTION2;
	}
	else if (choice == 3)
	{
		bitsPerSec = SPEED_OPTION3;
	}
	else if (choice == 4)
	{
		bitsPerSec = SPEED_OPTION4;
	}
	else if (choice == 5)
	{
		bitsPerSec = SPEED_OPTION5;
	}
	else
	{   //invalid choice - print messag and quit
		cout << "Invalid choice - must be 1, 2, 3, 4, or 5" << endl;
		cout << "Available transmission speeds" << endl;
		cout << "1." << setw(8) << SPEED_OPTION1 << " bps" << endl;
		cout << "2." << setw(8) << SPEED_OPTION2 << " bps" << endl;
		cout << "3." << setw(8) << SPEED_OPTION3 << " bps" << endl;
		cout << "4." << setw(8) << SPEED_OPTION4 << " bps" << endl;
		cout << "5." << setw(8) << SPEED_OPTION5 << " bps" << endl;
		cout << "Enter choice: ";
		cin >> choice;
	}
	 
	//calculate the number of full packets and size of last partial packet
	numFullPackets = numberBytes / DATA_BYTES;
	bytesLastPacket = numberBytes % DATA_BYTES;
	
	//compute the time to transmit the full packets
	timeFull = static_cast<double> (PACKET_LENGTH * 8) / 
				static_cast<double> (bitsPerSec);
	timeTransmit = timeFull * static_cast<double> (numFullPackets);
	
	//add time for last packet - no time if last packet has 0 bytes
	if (bytesLastPacket > 0)
	{
		timeLast = static_cast<double> ((bytesLastPacket + OVERHEAD_BYTES) * 8)
				   / static_cast<double> (bitsPerSec);
	}
	else
	{
		timeLast = 0.0;
	}
	timeTransmit = timeTransmit + timeLast;
	
	//set global print options 
	cout << showpoint << fixed << setprecision(4);  
	  
	//print information on packets
	cout << endl;
	cout << "Packet length is " << PACKET_LENGTH << " bytes with " 
		 << OVERHEAD_BYTES << " bytes of overhead\n";
	cout << "Number of full packets: " << numFullPackets << endl;
	cout << "Number of data bytes in last packet: " << bytesLastPacket << endl;
 
	//print time for full and partial last packet   
	cout << endl << "Time to transmit a full packet is " << timeFull 
		 << " seconds" << endl;
	cout << "Time to transmit the last packet is " << timeLast 
		 << " seconds" << endl;
	
	//print time in minutes if more than 60 seconds
	if (timeTransmit < 60)
	{
		cout << "Time to transmit the message: " << timeTransmit 
			 << " seconds" << endl;
	}
	else
	{   //print time in minutes and seconds
		timeMin = static_cast<int> (timeTransmit / 60.0);
		timeSec = timeTransmit - static_cast<double> (timeMin) * 60.0;
		cout << "Time to transmit the message is " << timeMin << " minutes and " 
			 << timeSec << " seconds" << endl;	  
	}
	
	//freeze output window and terminate
	cout << endl;
	system("Pause"); 
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with if statement

#2 Joker51390  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Having trouble with if statement

Posted 28 April 2009 - 04:29 PM

nevermind, i read it wrong

This post has been edited by Joker51390: 28 April 2009 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 tootintorrey  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-March 08

Re: Having trouble with if statement

Posted 28 April 2009 - 04:31 PM

not positive but in the if statements do you need to break it apart, by this i mean physically saying unitfile equals K or unitfile equals k


if ( unitFile == 'K' || unitFile =='k' )




not positive but in the if statements do you need to break it apart, by this i mean physically saying unitfile equals K or unitfile equals k


if ( unitFile == 'K' || unitFile =='k' )



Was This Post Helpful? 0
  • +
  • -

#4 lalaithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 09

Re: Having trouble with if statement

Posted 28 April 2009 - 04:36 PM

I still don't get the error message to appear on either of those...
Was This Post Helpful? 0
  • +
  • -

#5 Joker51390  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Having trouble with if statement

Posted 28 April 2009 - 04:40 PM

   //get units for size of message
	cout << endl << "Enter units for size of message (K or M): ";
	cin >> unitFile;
	if ( unitFile == 'K' )
	{
		//units are K - compute file size
		numberBytes = size * KILO;
	}
	else if ( unitFile == 'k' )
	{
			//units are K - compute file size
			numberBytes = size * KILO;
	}
	else if ( unitFile == 'M')
	{
		//units are K - compute file size
		numberBytes = size * KILO;
	}
	else if ( unitFile == 'm')
	{
		//units are M - compute file size
		numberBytes = size * MEGA;
	}
	
	else
	{   //invalid input
		cout << "Invalid units for size of message - must be M or K" << endl;
		cout << endl << "Enter units for size of message (K or M): ";
		cin >> unitFile;
	}



just change the Kilo in the 'M' because i copy and pasted the Kilo

This post has been edited by Joker51390: 28 April 2009 - 04:41 PM

Was This Post Helpful? 1
  • +
  • -

#6 lalaithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 09

Re: Having trouble with if statement

Posted 28 April 2009 - 04:54 PM

This one works, thanks!

View PostJoker51390, on 28 Apr, 2009 - 05:40 PM, said:

   //get units for size of message
	cout << endl << "Enter units for size of message (K or M): ";
	cin >> unitFile;
	if ( unitFile == 'K' )
	{
		//units are K - compute file size
		numberBytes = size * KILO;
	}
	else if ( unitFile == 'k' )
	{
			//units are K - compute file size
			numberBytes = size * KILO;
	}
	else if ( unitFile == 'M')
	{
		//units are K - compute file size
		numberBytes = size * KILO;
	}
	else if ( unitFile == 'm')
	{
		//units are M - compute file size
		numberBytes = size * MEGA;
	}
	
	else
	{   //invalid input
		cout << "Invalid units for size of message - must be M or K" << endl;
		cout << endl << "Enter units for size of message (K or M): ";
		cin >> unitFile;
	}



just change the Kilo in the 'M' because i copy and pasted the Kilo

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1