GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2007, 09:31
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

C++ student needs helps ASAP


I have been working on these programs for three days straight now. I have searched all over the internet for help but I can't seem to find any. I have the three programs written but I'm not getting the answers I'm supposed to be getting.

The first assignment is: Write a C++ program that accepts a string from the user and then replaces all occurrences of the letter e with the letter x.

And here is what I've come up with...

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{   
  string message;
  int e, x;
           
  cout  << "Enter a complete sentence: ";
  getline (cin, message);
  cout << "The sentence you just entered has been revised to: " << endl;

  string replace (e, x); 
  cout << message << endl;  
 
  return 0;
}  


The program runs, but when I enter a sentence, nothing changes.

The second assignment is: a. Write a function named countlets ( ) that returns the number of letters in a string passed as an argument. Digits, spaces, punctuation, tabs and newline characters should not be included in the returned count.

b. Include the countlets ( ) method written for Exercise 6a in an executable C++ program and use the program to test the method.

And here is what I've come up with:

CPP / C++ / C Code:
#include <iostream> 
#include <string> 
using namespace std;

int main()
{
    string str = "This 123/ is 567 A ?<6245> Test!";
    char nextChar;
    int i;
    int numLetters = 0;

    cout << "The original string is: " <<  str.length() 
		<<  " characters," <<  " which consist of" << endl; 

    // check each character in the string
    for (i = 0; i < str.length(); i++)
    {
      nextChar = str.at(i);  // get a character
      if (isalpha(nextChar))
        numLetters++; 

    cout << "  " <<  numLetters <<  " letters" << endl;

   cin.ignore();
   return 0;
}
}

I am not sure where to use countlets () and the result I get when I run the program is: The original string is: 32 characters, which consist of
1 letters

The third assignment is: Write a C++ program that accepts a string from the console and displays the string one word per line.

I have no idea how to approach this. I wrote what I thought was correct but I got a bunch of errors.

This is what I came up with:
CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;

 int main() {
      string s1("Test1 ");
      string s2("Test2 ");
      string s3("Test2 ");
      s1 = s1 + s2;
      cout << s1 << endl;
     s1 += s3;
      cout << s1 << endl;
      s1 += s3 + s3[4] + "Test4";
      cout << s1 << endl;

Any help would be appreciated. I'm really trying to understand this programming. If anyone can give me a step by step on what I'm doing wrong and explain my errors it would help a lot. Thanks in advance.
  #2  
Old 06-Mar-2007, 10:01
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: C++ student needs helps ASAP


For your first program, you're using replace incorrectly. The replace function replaces a string for a set of characters from a specified index and continuing for a specified length. Confused? I'll try an example.

CPP / C++ / C Code:
string s = "hello";
s.replace(0, 1, "j"); //"hello" is now "jello"
s.replace(1, 4, "ava"); //"jello" is now "java"

So basically, you specify where to begin the replacement (the first argument), and the next n letters (the second argument) are replaced with a string (the third argument).

Since this only works for one segment at a time, you'll need a loop.
CPP / C++ / C Code:
for (int i = 0; i < message.length(); i++)
{
     if (message[i] == 'e')
         s.replace(i, 1, "x");
}

So you need to take out the declaration of e and x (not sure what you were trying to accomplish with those anyway) and put the above loop in place of your replace statement.

In your second program, the structure is way off. You never end your for loop, and as a result you put two brackets at the end of the program (I assume make the compiler happy).

Your program should look like this:
CPP / C++ / C Code:
#include <iostream> 
#include <string> 
using namespace std;

int countlets(string str)
{
    int numLetters = 0;
    char nextChar;

    // check each character in the string
    for (i = 0; i < str.length(); i++)
    {
      nextChar = str.at(i);  // get a character
      if (isalpha(nextChar))
        numLetters++;
    }

    return numLetters;
}

int main()
{
    string str = "This 123/ is 567 A ?<6245> Test!";

    cout << "The original string is: " <<  str.length() 
		<<  " characters," <<  " which consist of " << endl; 

    cout << countlets(str);

    cout <<  " letters" << endl;

   cin.ignore();
   return 0;
}

The for loop could be simplified to
CPP / C++ / C Code:
    for (i = 0; i < str.length(); i++)
    {
      if (isalpha(str[i]))
         numLetters++;
    }
You wouldn't need to declare nextChar then. Also, in the main function, you don't need to declare i, since you're just doing it in the for loop. I assume that isalpha is defined somewhere? Otherwise the program obviously won't give correct results.

For the third program, use the string replace function again. However, this time, replace ' ' with "\n". This will force a new line between each word.

Hope this helps!
__________________
Gamer_2k4
  #3  
Old 06-Mar-2007, 11:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ student needs helps ASAP


Quote:
Originally Posted by silverneon
And here is what I've come up with...

CPP / C++ / C Code:
.
.
.
  string replace (e, x); 
.
.
.

The program runs, but when I enter a sentence, nothing changes.
This statement decfines a new variable named "replace". Its type is string. It is initialized as follows:
1. The length of the new string is made equal to the whatever the value of the int variable e is.
2. All chars in the string are given a value of whatever the value of char(x) is.

Since the values of the variables e and x are undefined in your program, the string is undefined. Since you don't try to do anything with the new string, maybe the program won't crash (or, maybe it could --- I really don't know), but one thing I would bet on: it won't do anything useful to your assignment.

Why not simply step through your message and see if each char is equal to 'e'? If it is, then replace it with 'x', otherwise, leave it alone.

CPP / C++ / C Code:
    for(unsigned i = 0; i < message.size(); i++) {
        // if value of message[i] is equal to 'e', then set it equal to 'x'
    }

Regards,

Dave
  #4  
Old 06-Mar-2007, 12:06
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

Re: C++ student needs helps ASAP


I completed the first problem (the e, x program). I go the second problem to work and here's what I've come up with:

CPP / C++ / C Code:
#include <iostream> 
#include <string> 
using namespace std;

int main()
{
    string str = "This 123/ is 567 A ?<6245> Test!";
    char nextChar;
    int i;
    int numLetters = 0;

    for (i = 0; i < str.length(); i++)
    {
      nextChar = str.at(i); 
      if (isalpha(nextChar))
        numLetters++; 
}
    cout << "The original string contains " <<  numLetters 
		<<  " letters." << endl; 


   cin.ignore();
   return 0;

}

It works but I'm not sure what to do with countlets ( ) ....any suggestions?
  #5  
Old 06-Mar-2007, 12:53
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: C++ student needs helps ASAP


Make a new function countlets that takes a strung and then use that for loop
  #6  
Old 06-Mar-2007, 12:54
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: C++ student needs helps ASAP


Quote:
Originally Posted by silverneon
It works but I'm not sure what to do with countlets ( ) ....any suggestions?

Try something like this ( note I didn't change any actual code, just rearranged it so that you are using a separate function, instead of having it all in main() ):

CPP / C++ / C Code:
#include <iostream> 
#include <string> 
using namespace std;
int countlets( const string & s ) {
    char nextChar;
    int i;
    int numLetters = 0;

    for (i = 0; i < s.length(); i++)
    {
      nextChar = s.at(i); 
      if (isalpha(nextChar))
        numLetters++; 
    }
    return numLetters;
}

int main()
{
    string str = "This 123/ is 567 A ?<6245> Test!";

    cout << "The original string contains " <<  countlets( str ) 
		<<  " letters." << endl; 


   cin.ignore();
   return 0;

}

Personally, I like this as a first pass on the function countlets():

CPP / C++ / C Code:
int countlets( const string & s ) {
  int x = 0;
  for( unsigned int i = 0; i < s.length(); i++ ) {
    if ( isalpha(s.at(i)) )  x++;
  }
  return x;
}

less ... messy.
  #7  
Old 06-Mar-2007, 12:59
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

Re: C++ student needs helps ASAP


Quote:
Originally Posted by darelf
Try something like this ( note I didn't change any actual code, just rearranged it so that you are using a separate function, instead of having it all in main() ):

CPP / C++ / C Code:
#include <iostream> 
#include <string> 
using namespace std;
int countlets( const string & s ) {
    char nextChar;
    int i;
    int numLetters = 0;

    for (i = 0; i < s.length(); i++)
    {
      nextChar = str.at(i); 
      if (isalpha(nextChar))
        numLetters++; 
    }
    return numLetters;
}

int main()
{
    string str = "This 123/ is 567 A ?<6245> Test!";

    cout << "The original string contains " <<  countlets( str ) 
		<<  " letters." << endl; 


   cin.ignore();
   return 0;

}

I just ran what you posted and it compiled with a bunch of errors.
  #8  
Old 06-Mar-2007, 13:04
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: C++ student needs helps ASAP


Quote:
Originally Posted by silverneon
I just ran what you posted and it compiled with a bunch of errors.

I dunno, it compiles and runs for me.... ( the version you quoted was just before I edited it to fix a typing mistake..... try it with what's there now...)
  #9  
Old 06-Mar-2007, 13:09
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

Re: C++ student needs helps ASAP


I dunno..I run it and get this:

CPP / C++ / C Code:
test1.cpp: In function `int countlets(const std::string&)':
test1.cpp:11: error: `str' undeclared (first use this function)
test1.cpp:11: error: (Each undeclared identifier is reported only once for each 
   function it appears in.)

I'm probably the one messing something up.
  #10  
Old 06-Mar-2007, 13:23
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: C++ student needs helps ASAP


Quote:
Originally Posted by silverneon
I dunno..I run it and get this:

CPP / C++ / C Code:
test1.cpp: In function `int countlets(const std::string&)':
test1.cpp:11: error: `str' undeclared (first use this function)
test1.cpp:11: error: (Each undeclared identifier is reported only once for each 
   function it appears in.)

I'm probably the one messing something up.

Yeah, in the function "countlets" where it says "str" change it to "s"
CPP / C++ / C Code:
//instead of:
nextChar = str.at(i);
//it should be:
nextChar = s.at(i);
.... that was a typo on my part.... totally my fault.
 
 

Recent GIDBlogPh.D. progress by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
C Code i need help for printing batman3280 C Programming Language 8 13-Mar-2006 14:48
Help in C Print is not working with LinkList batman3280 C Programming Language 3 09-Mar-2006 19:03
Problem with program breggo C++ Forum 3 08-Jun-2005 13:51
MultiDimensional Array from a .dat file joeyz C Programming Language 2 05-May-2005 19:26
Operator overloading (not happening) gmn C++ Forum 11 30-Aug-2003 08:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:52.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.