![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
Got stuck while making a program,help would be appreciated
I decided to make a program that would change the inserted text according to this pattern:Replacing every letter with the next one in the alphabet.For example:
"Cat"- "Dbu". I started and it went well up untill a point,my code looks like this so far: c++ Syntax (Toggle Plain Text)
It seemed to work well,but when i tried to make the program to replace the letter "B" with "C",it replaced the allready replaced letter "B".I am a tottal begginer in C++ and with no experience in other programming languages so far. I found help some time ago here on another question,i sincerely hope that you guys will be helpfull again. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Re: Got stuck while making a program,help would be appreciated
My guess is that you added another loop similar to the one you are using there, so firstly it proccesses all the and and changes them to b's, then goes back to the start of the string and checks for all the b's and changes them to c's. See where you are going wrong?
Personally i would try a slightly different approach which is much less code. Im sure you are aweare tha each character in the alphat bet is represented by a number depending on which encoding you are using for example ASCII 'a' is 97. ASCII 'b' is 98. using this logic we can rewrite you code to change all the letters to the next letter in the alphabet in 1 loop. I would change your for loop to something like this. C++ Syntax (Toggle Plain Text)
Now firstly you will notice that the conition of the for loop has been change from x<100; to x<(zbor.length());, this means that the for loop will only iterate as many times as there are number of characters in the string.char((int(zbor[x])+1)). This may look a bit confusing if your new to C++ but its quite simple.firstly it changes the letter at zbor[x] to an integer, then adds one to this to get the next letter in the alphabet then the outside char() converts the number back into its character form before finally storing it back into zbor[x].Note this is only a quick solution and not a full solution. AS i have left a few things out for you to think about, 1) what happens ro any space's in the string? 2) Whats happens when you reach 'z'? 3) Other punctuation, will not remain the same. So you made need to add in a few more lines of code to check that it is a letter, and then when you reach z, is the next ccharacter in the alphabet A?. A small clue for you is that all of the characters in the alphabet lie between the number 65 (A) and 122 (z), assuming its ASCII. So perhaps you should check to see if the value of the letter is within there before changing it. Hope i didn't talk to much rubbish. Chris
__________________
Steven Skiena - Algorithms ,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],brainf**k -- It's such a pretty language |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Re: Got stuck while making a program,help would be appreciated
To deal with some of the stuff Chris left you to think about, you might consider some of the functions available from the header <cctype>. isalpha() for instance will be easier to use than checking that the integer value of the character is between 65 and 122, since there are a couple characters in there which aren't letters. isupper() or islower() can quickly tell you the casing of the letter as well (though you can come up with a solution where the case doesn't matter; I leave that as a challenge to you
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
Re: Got stuck while making a program,help would be appreciated
Thanks guys i will get to work at once
|
|
|
|
|
|
#5 |
|
Professional Programmer
|
Re: Got stuck while making a program,help would be appreciated
let us know how it turns out
![]()
__________________
Steven Skiena - Algorithms ,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],brainf**k -- It's such a pretty language |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Re: Got stuck while making a program,help would be appreciated
use an if statement to verify that the input is between a and z. you might need two conditions (an || ) if you're also accepting A-Z. I don't remember exactly but its in the ASCII table. C++ might have a function that skips whitespace. If not you can easily make your own by reading the input into a String, then 'deleting' any whitespace. you could do this by making a char var = " "; then trying to match it against each part of the String.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
Re: Got stuck while making a program,help would be appreciated
I managed to build this so far,the problem with typing other characters is solved,they remain the same as intended,but there is a problem with spaces and the letter z
![]() code c++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#8 |
|
Professional Programmer
|
Re: Got stuck while making a program,help would be appreciated
The reason why your having trouble with space is not to do with the code that is changing the letter, but the method that you accept input.
cin only accepts 1 word upto a space character, so instead you should implement something like this C++ Syntax (Toggle Plain Text)
EDIT: this is perhaps the better way ![]() C++ Syntax (Toggle Plain Text)
im really not thinking straight tonight getline() Chris
__________________
Steven Skiena - Algorithms ,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],brainf**k -- It's such a pretty language |
|
|
|
|
|
#9 | |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
Re: Got stuck while making a program,help would be appreciated
Quote:
Yes dude thank you,you are tottaly right,what was i thinking,i guess i am not thinking at all tonight,will fix that now,going to bed to be as fresh as i can be for tomorow. |
|
|
|
|
|
|
#10 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Re: Got stuck while making a program,help would be appreciated
The problem with 'z' is a logic check you need to make. It's a special case, since incrementing it the value doesn't loop around again. You'll probably want another if statement for this case.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help. I got a problem in making Java games .. | bratsercom | Java | 11 | Oct 1st, 2007 3:31 PM |
| C++ Getting stuck within a loop. | n3o_X | C++ | 5 | Aug 29th, 2007 3:03 PM |
| Tutorials for making a P2P program? | Palladio | C++ | 3 | Nov 26th, 2005 4:51 AM |
| Multiple http-requests are stuck | MereMortal | C++ | 0 | May 4th, 2005 4:08 AM |
| Making a Note Adder/ Editor/ Deleter/ Viewer thingy | brokenhope | C++ | 19 | Apr 29th, 2005 2:30 AM |