p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

c_plus_plus_programming thread: SV: Beginner drowning Please help!


Message #1 by Jesper Nielsen <JNN@d...> on Tue, 5 Feb 2002 11:34:46 +0100
This message is in MIME format. Since your mail reader does not understand

this format, some or all of this message may not be legible.



------_=_NextPart_001_01C1AE30.BEC4BDF4

Content-Type: text/plain



Hi,

Try this

***********

	const int count = 10; // size of string array

	string words[count]; 

	

	char finishLoop = 'n';

	int counter = 0;

	do

	{

		cout << "Enter a name: " << endl;

		cin >> words[counter++]; // indicates which index you are

placing your input. 

		



		cout << "Enter another name (y/n)?" << endl;

		cin >> finishLoop;

	}while(tolower(finishLoop) =='y' && counter < count);



	return 0;

****************

regards 

Jesper

> -----Oprindelig meddelelse-----

> Fra:	wigardrobert@a... [SMTP:wigardrobert@a...]

> Sendt:	5. februar 2002 01:34

> Til:	C++_Programming

> Emne:	[c_plus_plus_programming] Beginner drowning Please help!

> 

> I am a beginning C++ student. I am trying to create a string array ( for a

> 

> practice exercise)that will allow a teacher to enter

> 10 student names. I can't get the proper balence to 1. Allow a const to 

> limit the entries to 10.

> 2. Establish an array of type string tha t will operate in a loop to allow

> 

> multiple entries. 3. I

> have no idea what input operator to use for this ( cin, cin.getline, 

> getline() and what the proper arguments are that will allow it to compile.

> Wrox provides a solution ( This is from Ivor Horton's Beginning C++ The 

> Complete Language Chapter 6 end of chapter exercise

> 6.1) that compiles, but it fails at execution with an Unhandled 

> exception / access violation.

> Please help- I don't want to feel retarded! Below is as close I can get 

> and the compiling errors, which I don't understand too much.

> #include <iostream>

> #include <string>

> 

> using namespace std;

> 

> int main()

> {

> 	string words[] = {0};

> 	const int count = 10;

> 	char finishLoop = 'n';

> 	int counter = 0;

> 

> 	do

> 	{

> 		cout << "Enter a name: " << endl;

> 		getline( cin, words);

> 		counter++;

> 

> 		cout << "Enter another name (y/n)?" << endl;

> 		cin >> finishLoop;

> 	}while(tolower(finishLoop) =='y' && counter < count);

> 

> 	return 0;

> }

> 

> 

> 

> Compiling...

> stringWordsd.cpp

> F:\MSDev98\MyProjects\Chapter6\ArraysOfTypeString\stringWordsd.cpp(16) : 

> error C2664: 'class std::basic_istream<char,struct std::char_traits<char> 

> > &__cdecl std::getline(class std::basic_istream<char,struct 

> std::char_traits<char> > &,class std::bas

> ic_string<char,struct std::char_traits<char>,class std::allocator<char> > 

> &)' : cannot convert parameter 2 from 'class std::basic_string<char,struct

> 

> std::char_traits<char>,class std::allocator<char> > [1]' to 'class 

> std::basic_string<char,struct std

> ::char_traits<char>,class std::allocator<char> > &'

>         A reference that is not to 'const' cannot be bound to a non-lvalue

> Error executing cl.exe.

> 

> stringWordsd.obj - 1 error(s), 0 warning(s)

> 

> 

> 

> #include <iostream>

> #include <string>

> 

> using namespace std;

> 

> int main()

> {

> 	string words[] = {0};

> 	const int count = 10;

> 	char finishLoop = 'n';

> 	int counter = 0;

> 

> 	do

> 	{

> 		cout << "Enter a name: " << endl;

> 		cin >> words;

> 		counter++;

> 

> 		cout << "Enter another name (y/n)?" << endl;

> 		cin >> finishLoop;

> 	}while(tolower(finishLoop) =='y' && counter < count);

> 

> 	return 0;

> }

> 

> 

> Compiling...

> stringWordsd.cpp

> F:\MSDev98\MyProjects\Chapter6\ArraysOfTypeString\stringWordsd.cpp(16) : 

> error C2679: binary '>>' : no operator defined which takes a right-hand 

> operand of type 'class std::basic_string<char,struct 

> std::char_traits<char>,class std::allocator<char> >

>  [1]' (or there is no acceptable conversion)

> Error executing cl.exe.




> $subst('Email.Unsub').




Message #2 by "Thoyd Loki" <wigardrobert@a...> on Tue, 5 Feb 2002 20:57:06 -0800
This is a multi-part message in MIME format.



------=_NextPart_000_0011_01C1AE87.AB8C9660

Content-Type: text/plain;

	charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable



SV: [c_plus_plus_programming] Beginner drowning Please help!Thank YOU

  ----- Original Message -----

  From: Jesper Nielsen

  To: C++_Programming

  Sent: Tuesday, February 05, 2002 02:34

  Subject: [c_plus_plus_programming] SV: Beginner drowning Please help!





  Hi,

  Try this

  ***********

          const int count =3D 10; // size of string array

          string words[count];

         

          char finishLoop =3D 'n';

          int counter =3D 0;

          do

          {

                  cout << "Enter a name: " << endl;

                  cin >> words[counter++]; // indicates which index you 

are placing your input.

                 



                  cout << "Enter another name (y/n)?" << endl;

                  cin >> finishLoop;

          }while(tolower(finishLoop) =3D=3D'y' && counter < count);



          return 0;

  ****************

  regards

  Jesper



    -----Oprindelig meddelelse-----

    Fra:    wigardrobert@a... [SMTP:wigardrobert@a...]

    Sendt:  5. februar 2002 01:34

    Til:    C++_Programming

    Emne:   [c_plus_plus_programming] Beginner drowning Please help!



    I am a beginning C++ student. I am trying to create a string array ( 

for a

    practice exercise)that will allow a teacher to enter

    10 student names. I can't get the proper balence to 1. Allow a const 

to

    limit the entries to 10.

    2. Establish an array of type string tha t will operate in a loop to 

allow

    multiple entries. 3. I

    have no idea what input operator to use for this ( cin, cin.getline, 



    getline() and what the proper arguments are that will allow it to 

compile.

    Wrox provides a solution ( This is from Ivor Horton's Beginning C++ 

The

    Complete Language Chapter 6 end of chapter exercise

    6.1) that compiles, but it fails at execution with an Unhandled

    exception / access violation.

    Please help- I don't want to feel retarded! Below is as close I can 

get

    and the compiling errors, which I don't understand too much.

    #include <iostream>

    #include <string>



    using namespace std;



    int main()

    {

            string words[] =3D {0};

            const int count =3D 10;

            char finishLoop =3D 'n';

            int counter =3D 0;



            do

            {

                    cout << "Enter a name: " << endl;

                    getline( cin, words);

                    counter++;



                    cout << "Enter another name (y/n)?" << endl;

                    cin >> finishLoop;

            }while(tolower(finishLoop) =3D=3D'y' && counter < count);



            return 0;

    }









    Compiling...

    stringWordsd.cpp

    

F:\MSDev98\MyProjects\Chapter6\ArraysOfTypeString\stringWordsd.cpp(16) : 



    error C2664: 'class std::basic_istream<char,struct 

std::char_traits<char>

    > &__cdecl std::getline(class std::basic_istream<char,struct

    std::char_traits<char> > &,class std::bas

    ic_string<char,struct std::char_traits<char>,class 

std::allocator<char> >

    &)' : cannot convert parameter 2 from 'class 

std::basic_string<char,struct

    std::char_traits<char>,class std::allocator<char> > [1]' to 'class

    std::basic_string<char,struct std

    ::char_traits<char>,class std::allocator<char> > &'

            A reference that is not to 'const' cannot be bound to a 

non-lvalue

    Error executing cl.exe.



    stringWordsd.obj - 1 error(s), 0 warning(s)









    #include <iostream>

    #include <string>



    using namespace std;



    int main()

    {

            string words[] =3D {0};

            const int count =3D 10;

            char finishLoop =3D 'n';

            int counter =3D 0;



            do

            {

                    cout << "Enter a name: " << endl;

                    cin >> words;

                    counter++;



                    cout << "Enter another name (y/n)?" << endl;

                    cin >> finishLoop;

            }while(tolower(finishLoop) =3D=3D'y' && counter < count);



            return 0;

    }







    Compiling...

    stringWordsd.cpp

    

F:\MSDev98\MyProjects\Chapter6\ArraysOfTypeString\stringWordsd.cpp(16) : 



    error C2679: binary '>>' : no operator defined which takes a 

right-hand

    operand of type 'class std::basic_string<char,struct

    std::char_traits<char>,class std::allocator<char> >

     [1]' (or there is no acceptable conversion)

    Error executing cl.exe.




$subst('Email.Unsub').






$subst('Email.Unsub').







  Return to Index