Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 15th, 2005, 01:35 PM
RCB RCB is offline
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Confusing problem in C++

please help me with this
I'm having problem with this code
what im trying to do is ask the user to type a line
and i read it as a character, one character at a time. no strings or arrys ..
if the line has a colon in it, it should be changed to a period.
and if there is 'and' after the colon then it should be removed and capitalize the first letter of the next work, if there is no 'and' then just capitalize the word after the colon.
i managed to change the colon to a period but i am having problem making it recognize the word and
 my code
int main()
{

 char c, colon=',';
 ch= cin.get();
 while (ch = cin.get())
  {
    if (ch == colon)
    {
        ch ='.';
        if (ch == 'a')
           {
            if (ch=='n')
             {
              if (ch=='d')
                {
                 cin.ignore();
                 }
               }
            }
      }
     cout << ch;
   }
}

Reply With Quote
  #2 (permalink)  
Old February 16th, 2005, 06:54 AM
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi
  I hope the following modifications in ur code will solve ur problem.
#include<iostream.h>
#include<ctype.h>
void main()
{
 char ch, colon=':',tmp,tmp1,tmp2,tmp3;
 ch= cin.get();
 int i;
 while (ch!='\n')
  {
  if (ch == colon)
    {
    ch ='.';
    cout<<ch;
    ch=cin.get();
    tmp=ch;
    if (ch == 'a')
    {
        Â ch=cin.get();
        Â tmp1=ch;
        Â if (ch=='n')
        Â {
        Â Â ch=cin.get();
        Â tmp2=ch;
        if (ch=='d')
        Â {
       cin.ignore();/*if there is space after and use this line*/
        Â ch=cin.get();
        Â i=toupper(ch);
        Â ch=(char)(i);
        Â cout<<ch;
        Â Â }
        Â else
        Â Â {
        Â Â Â i=toupper(tmp);
        Â Â Â ch=(char)(i);
        Â Â Â cout<<ch;
        Â Â Â cout<<tmp1;
        Â Â Â cout<<tmp2;

        Â Â }
        Â Â }
        Â Â else
        Â Â {
        i=toupper(tmp);
        ch=(char)(i);
        cout<<ch;
        cout<<tmp1;

        Â Â }

    }
    Â Â Â else
    Â Â Â {
    i=toupper(tmp);
    ch=(char)(i);
    cout<<ch;
    }
    }
  else
   {
    Â cout<<ch;
   }

   ch=cin.get();

    }
}
This is not the only solution..there may be many solutions better than this....
Reply With Quote
  #3 (permalink)  
Old February 26th, 2005, 12:20 PM
Authorized User
 
Join Date: Feb 2005
Location: Mumbai, Maharashtra, India.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this modification might solve ur prob

int main()
{

 char c, colon=',';
 ch= cin.get();
 while (ch = cin.get())
  {
    if (ch == colon)
    {
        ch ='.';
        ch=cin.get();
        if (ch == 'a')
           {
             ch=cin.get();
             if (ch=='n')
             {
              ch=cin.get();
              if (ch=='d')
                {
                 cin.ignore();
                 }
               }
            }
      }
     cout << ch;
   }
}



punkaj
Reply With Quote
  #4 (permalink)  
Old February 26th, 2005, 07:35 PM
Authorized User
 
Join Date: Oct 2004
Location: Fayetteville, AR, USA.
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

here are some mods that work

# include <iostream.h>
# include <ctype.h>



int main()
{
    char ch;
    ch=cin.get();
    do
    {
        switch(ch)
        {
        case 'A':
        case 'a':
        case ':':
            {
                // If colon
                if(ch==':')
                {
                    // change colon to period
                    cout<<". ";
                    // check to see if the next char is a space or character
                    if((ch=cin.peek())==32)
                    {
                        // Found white space ignore and get next character
                        cin.ignore();
                        ch=cin.get();
                    }
                    else
                        // Not a white space get next charater
                        ch=cin.get();
                }

                // check for the word "and"
                if(ch=='a'||ch=='A')
                {
                    if((ch=cin.get())=='n'||ch=='N')
                        if((ch=cin.get())=='d'||ch=='D')
                        {
                            // check for white space following "and"
                            if((ch=cin.peek())==32)
                                // If white space ignore
                                cin.ignore();
                            else
                                // character found insert space
                                cout<<" ";
                        }
                }
                // "and" deleted and/or next word capitalized
                cout<<(ch=toupper(ch=cin.get()));
            }
            break;
        default:
            { // print out every other leter
                cout<<ch;
            }
        }// get next character
    }while(ch=cin.get());
    return 1;
}
Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
confusing in ADO.NET Entity Data Model,Linq to sql angelboy ADO.NET 5 November 21st, 2008 05:01 PM
Simple yet confusing jack123 SQL Server 2000 1 July 21st, 2007 01:24 PM
WOW--TIO-0905 VERY confusing rsearing BOOK: Beginning ASP.NET 2.0 and Databases 9 October 12th, 2006 12:36 PM
class and struct variables are confusing webworldman C# 2 August 7th, 2006 03:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.