Wrox Programmer Forums
|
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 December 25th, 2006, 07:00 PM
bLo bLo is offline
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mysterious Coding

Dear All,

Whatz wrong with this code :

//Compile using minGW 2.05
//project x-1
// coding myterious
//patch no
//boolean true|false ???

#include <iostream>

using namespace std;

void sample_void_1() // learn using void
{

    string name;
    char op = 'y';

    do
    {

    cout << " Please type ur name : ";
    getline(cin, name);

    cout << " Hallo " << name << endl;

    cout << " Again ( Y / N )";
    cin >> op;


    }
    while((op=='y')||(op=='Y'));
}

int main()
{
 sample_void_1();

 return 0;
 }



Result:

 Please type ur name : rere
 Hallo rere
 Again ( Y / N )y
 Please type ur name : Hallo
 Again ( Y / N )y
 Please type ur name : Hallo
 Again ( Y / N )n


Terminated with return code 0
Press any key to continue ...



If im using like this no problem :


#include <iostream>

using namespace std;

int main()
{

    float a,b,result;
    char op; // operator y or n

    do {
    cout << "First number: ";
    cin >> a;

    cout << "Second number: ";
    cin >> b;

    result = a * b ;

    cout << "" << result << endl;

    cout << " Again ( Y / N )";
    cin >> op;

        }
        while((op=='y')||(op=='Y'));


    return 0;
}

Result:

First number: 6
Second number: 7
42
 Again ( Y / N )y
First number: 5
Second number: 5
25
 Again ( Y / N )n


Terminated with return code 0
Press any key to continue ...

Reply With Quote
  #2 (permalink)  
Old December 26th, 2006, 08:37 AM
Authorized User
 
Join Date: Oct 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi blo,

   what is your problem with your code? what do you want to be the output of your code?

___________________________________________
.::If you quit you loss, If you survive you win::.

.::JHANNY::.

Reply With Quote
  #3 (permalink)  
Old December 27th, 2006, 02:47 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

Extraction stops for the operator>> for a couple of resons one of which is:

After the function extracts an element ch for which use_facet<ctype<CharType> >( getloc). is( ctype<CharType>::space, ch) is true, in which case the character is put back.

If the function extracts no elements, it calls setstate(ios_base::failbit). In any case, it calls istr.width(0) and returns *this.

when using the cin operator>> to extract data, a newline character is being left in the buffer. so on its next consecutive read with the string class helper function getline(), the fail bit flag is being set and your string is set to an empty status. try suing the following

// use a cin.ignore() after extracting the continue option
    cin >> op;
    if ( cin.peek()=='\n' )
        cin.ignore();
Reply With Quote
  #4 (permalink)  
Old January 4th, 2007, 01:22 AM
bLo bLo is offline
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear C@uark,

Sori lately reply ur messages.
Internet in my place is down.

Btw,

im already try ur code, n' it's working properly!
Thanks Bro :D

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
mysterious problem (data no inserting ) gratisaccount VB.NET 2002/2003 Basics 1 June 29th, 2006 08:48 AM
mysterious error runtime error '451' coyotworks Excel VBA 1 May 12th, 2006 03:57 PM
Help coding here Scoob PHP How-To 3 January 26th, 2006 11:18 AM
Mysterious FileSysObj Behavior mdoleman Access VBA 1 April 26th, 2005 04:55 PM
Mysterious Hidden OrderByOn tlwaltz BOOK: Expert One-on-One Access Application Development 0 October 11th, 2004 06:52 PM





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