Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2008, 1:12 PM   #1
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
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)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void Funkcija1(string zbor)
  6. {
  7. char x;
  8. zbor[x];
  9. for(x=0;x<100;x++)
  10. {
  11. if(zbor[x]=='a')
  12. zbor[x]='b';
  13. }
  14.  
  15. cout<<zbor<<endl;
  16.  
  17. }
  18.  
  19. int main()
  20.  
  21. {
  22. string Vs;
  23. cin>>Vs;
  24. Funkcija1(Vs);
  25.  
  26. system("PAUSE");
  27.  
  28. return EXIT_SUCCESS;
  29.  
  30. }

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.
Marijan is offline   Reply With Quote
Old Mar 30th, 2008, 1:36 PM   #2
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 448
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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)
  1. for(x=0;x<(zbor.length());x++)
  2. {
  3. zbor[x]=char((int(zbor[x])+1));
  4. }

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
Freaky Chris is online now   Reply With Quote
Old Mar 30th, 2008, 3:08 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Old Mar 30th, 2008, 3:29 PM   #4
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: Got stuck while making a program,help would be appreciated

Thanks guys i will get to work at once
Marijan is offline   Reply With Quote
Old Mar 30th, 2008, 4:34 PM   #5
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 448
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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
Freaky Chris is online now   Reply With Quote
Old Mar 30th, 2008, 4:49 PM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Mar 30th, 2008, 5:54 PM   #7
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: Got stuck while making a program,help would be appreciated

Quote:
Originally Posted by Freaky Chris View Post
let us know how it turns out
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)
  1. #include <iostream>
  2. #include <string>
  3. #include <ctype.h>
  4. using namespace std;
  5.  
  6. void Funkcija1(string zbor)
  7. { char a,b,c;
  8. char x;
  9. zbor[x];
  10. for(x=0;x<(zbor.length());x++)
  11. {
  12. if(isalpha(zbor[x]))
  13. zbor[x]=((int(zbor[x])+1));
  14.  
  15. }
  16.  
  17. cout<<zbor<<endl;
  18.  
  19. }
  20.  
  21. int main()
  22.  
  23. {
  24. string Vs;
  25. cin>>Vs;
  26. Funkcija1(Vs);
  27.  
  28. system("PAUSE");
  29.  
  30. return EXIT_SUCCESS;
  31.  
  32. }
Marijan is offline   Reply With Quote
Old Mar 30th, 2008, 6:08 PM   #8
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 448
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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)
  1. char Vs[200];
  2. cin.getline(Vs, 200);

EDIT: this is perhaps the better way

C++ Syntax (Toggle Plain Text)
  1. string Vs;
  2. getline(cin, Vs);

im really not thinking straight tonight

getline()
Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is online now   Reply With Quote
Old Mar 30th, 2008, 6:25 PM   #9
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: Got stuck while making a program,help would be appreciated

Quote:
Originally Posted by Freaky Chris View Post
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)
  1. char Vs[200];
  2. cin.getline(Vs, 200);

EDIT: this is perhaps the better way

C++ Syntax (Toggle Plain Text)
  1. string Vs;
  2. getline(cin, Vs);

im really not thinking straight tonight

getline()
Chris

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.
Marijan is offline   Reply With Quote
Old Mar 30th, 2008, 8:46 PM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC