switch or if else statement

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1707 Views - Last Post: 12 March 2008 - 12:15 PM Rate Topic: -----

#1 missy 69   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-March 08

switch or if else statement

Post icon  Posted 08 March 2008 - 04:00 PM

hi i have just been given an assignment i know what i want to do just need help with first statement


i need to change A to little a do i use an if else state ment
Is This A Good Question/Topic? 0
  • +

Replies To: switch or if else statement

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: switch or if else statement

Posted 08 March 2008 - 04:13 PM

You can use either.
Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble   User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: switch or if else statement

Posted 08 March 2008 - 05:27 PM

char letter1 = 'a'
char letter2 = 'B';

letter1 = letter1 - 32; //will convert 'a' to 'A'
letter2 = letter2 + 32; // will convert 'B' to 'b'




If you think we are helping you in the wrong direction, please post your code and explain in detail what you wish to do.

Welcome to </dream.in.code> Have a great time here :)
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: switch or if else statement

Posted 08 March 2008 - 05:44 PM

Whenever this topic (converting case) comes up I send people over to www.asciitable.com.

There are several ways that you can convert cases, one way is your use a conditional block (if-else-if or switch etc). The other way is to use a little math (see letthecolorsrumble post).
Was This Post Helpful? 0
  • +
  • -

#5 missy 69   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-March 08

Re: switch or if else statement

Posted 08 March 2008 - 11:20 PM

View Postletthecolorsrumble, on 8 Mar, 2008 - 05:27 PM, said:

char letter1 = 'a'
char letter2 = 'B';

letter1 = letter1 - 32; //will convert 'a' to 'A'
letter2 = letter2 + 32; // will convert 'B' to 'b'




If you think we are helping you in the wrong direction, please post your code and explain in detail what you wish to do.

Welcome to </dream.in.code> Have a great time here :)



So for every differnent letter i wishto change i must declare each one seperate?????????????????????????????????????????????????????????????????????????????????????????????????????????????? :D
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6790
  • View blog
  • Posts: 31,331
  • Joined: 10-May 07

Re: switch or if else statement

Posted 08 March 2008 - 11:23 PM

My preference:
toupper
tolower
Was This Post Helpful? 0
  • +
  • -

#7 zmikeb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-March 08

Re: switch or if else statement

Posted 09 March 2008 - 02:13 PM

View Postno2pencil, on 8 Mar, 2008 - 11:23 PM, said:

My preference:
toupper
tolower


I agree. This is the easiest way to do it. Just don't forget to #include <ctype.h> in your preprocessor directives.
Was This Post Helpful? 0
  • +
  • -

#8 missy 69   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-March 08

Re: switch or if else statement

Posted 10 March 2008 - 02:30 PM

View Postmissy 69, on 8 Mar, 2008 - 04:00 PM, said:

hi i have just been given an assignment i know what i want to do just need help with first statement


i need to change A to little a do i use an if else state ment

CAN SOME ONE PLEASE PUSH ME IN DIRECTION ON WHERE TO START I HAVE DONE MY ALGORITHM BUT I CANT CODE IT I WANT TO CHANGE ALL a TO A AND e TO E ETC CAN SOMEONE PROVIDE ME CODE TO CONVERT THE FIRST LETTER PLAESE ????????????????????????????????????????????????????????????????????????????????????????????????????????????
Was This Post Helpful? 0
  • +
  • -

#9 MorphiusFaydal   User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: switch or if else statement

Posted 10 March 2008 - 02:37 PM

Dear God. No need to yell. And no2pencil pretty much gave you the solution.
Was This Post Helpful? 0
  • +
  • -

#10 missy 69   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-March 08

Re: switch or if else statement

Posted 10 March 2008 - 05:47 PM

View PostMorphiusFaydal, on 10 Mar, 2008 - 02:37 PM, said:

Dear God. No need to yell. And no2pencil pretty much gave you the solution.






yes but the link doesnt actually convert out to screen i am really slow at this i have written my algorithm i want to follow but have no idea how to code this
Was This Post Helpful? 0
  • +
  • -

#11 MorphiusFaydal   User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: switch or if else statement

Posted 10 March 2008 - 06:57 PM

So store the result somewhere else.

char foo = 'a';
char bar;

bar = toupper(foo);

cout << foo << endl << bar << endl;



First week of CS kind of stuff.

This post has been edited by MorphiusFaydal: 10 March 2008 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#12 jhedz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-March 08

Re: switch or if else statement

Posted 11 March 2008 - 07:07 AM

missy if you want to change an uppercase letter to lowercase just use the tolower function()...okie
i hope i may gonna help you...
goodluck!!!

//Example reads in a character and makes up lowercase 
#include <iostream>//library for cout
#include <cctype>//librabry

using namespace std;//standard

int main()
{
	char x;//declaration variable for x
	cin>>x;//input the x
	x=tolower(x);//it will lowercase the uppercase letter
	cout<<x;//it will display the lowercase letter
}

Mod Edit: added code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: switch or if else statement

Posted 11 March 2008 - 07:09 AM

View Postmissy 69, on 10 Mar, 2008 - 08:47 PM, said:

View PostMorphiusFaydal, on 10 Mar, 2008 - 02:37 PM, said:

Dear God. No need to yell. And no2pencil pretty much gave you the solution.

yes but the link doesnt actually convert out to screen i am really slow at this i have written my algorithm i want to follow but have no idea how to code this

If you do not wish to follow the provided solution, and would perfer to code the algorithm you have designed, then please provide that algorithm.
Was This Post Helpful? 0
  • +
  • -

#14 Delta_Echo   User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: switch or if else statement

Posted 11 March 2008 - 08:32 AM

Or, if you want to you can try something like this:
#include <iostream>

using namespace std;

int main()
{		   char x[2];
			cout << "Input a letter: " cin >> x;
			cin.getline ( x, 2);

			if ( x = "A" )
			{   
				 x = "a"; 
			 }
			 if ( x = "a" )
			 {
				  x = "A";
			  }
			}	  
			  



I think thats a good example, IF you want to use an if statement.
I think you should just use the functions listed above. Or make one yourself, but that would be a waste of time.

Hope I helped, and please don't yell on the forum. We are here to help.

Live long and code strong!
Was This Post Helpful? 0
  • +
  • -

#15 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: switch or if else statement

Posted 11 March 2008 - 09:21 AM

View PostDelta_Echo, on 11 Mar, 2008 - 11:32 AM, said:

Or, if you want to you can try something like this:
#include <iostream>

using namespace std;

int main()
{		   char x[2];
			cout << "Input a letter: " cin >> x;
			cin.getline ( x, 2);

			if ( x = "A" )
			{   
				 x = "a"; 
			 }
			 if ( x = "a" )
			 {
				  x = "A";
			  }
			}	  
			  



I think thats a good example, IF you want to use an if statement.
I think you should just use the functions listed above. Or make one yourself, but that would be a waste of time.

Hope I helped, and please don't yell on the forum. We are here to help.

Live long and code strong!

You may wish to modify the if and else conditions to use the equality operator as opposed to the assignment operator. the use of the assingment operator will allow the code to run, but will not produce the expected result.

if ( x == "a" )


Also, only a single char variable would be needed, I would think, as opposed to the array, at which point one would use the single quote notation.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2