10 Replies - 691 Views - Last Post: 28 July 2011 - 12:25 PM Rate Topic: ***-- 2 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Character of a string | size_type Problem

Posted 27 July 2011 - 09:56 PM

Recently I was working out a project from the book C++ Primer and wrote the following code get all the characters of a string. Here's my code, which is a little modified than the book's code.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    cout << "Enter a line: " << endl;
    getline(cin, str);
    for(size_type ix=0; ix!=str.size(); ++ix)
    cout << str[ix] << endl;
    return 0;
}



These are the errors I get :

E:\C++\MiniGW C++\String Character Fetch\main.cpp||In function 'int main()':|
E:\C++\MiniGW C++\String Character Fetch\main.cpp|10|error: 'size_type' was not declared in this scope|
E:\C++\MiniGW C++\String Character Fetch\main.cpp|10|error: expected ';' before 'ix'|
E:\C++\MiniGW C++\String Character Fetch\main.cpp|10|error: 'ix' was not declared in this scope|
||=== Build finished: 3 errors, 0 warnings ===|


I can see the size_type is creating problem, but the book want's me to use the same !

I can see that size_t works, but are they the same?

Thanks !

This post has been edited by Jeet.in: 27 July 2011 - 09:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Character of a string | size_type Problem

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Character of a string | size_type Problem

Posted 27 July 2011 - 10:16 PM

Use string::size_type or str.size_type. I think it's usually the same as size_t, but it doesn't have to be.
Was This Post Helpful? 1
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Character of a string | size_type Problem

Posted 27 July 2011 - 10:24 PM

Thanks Hezekiah, str.size_type returns error, but string::size_type works perfectly.

This post has been edited by Jeet.in: 27 July 2011 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Character of a string | size_type Problem

Posted 27 July 2011 - 10:27 PM

I thought one of them might not work, so I posted both. :)
Was This Post Helpful? 1
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Character of a string | size_type Problem

Posted 27 July 2011 - 10:34 PM

Noticed one strange thing, even if I use using std::string
and compile the code as size_type (stripping the string::), it still returns an error. Any idea why so?
Was This Post Helpful? 0
  • +
  • -

#6 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Character of a string | size_type Problem

Posted 27 July 2011 - 10:38 PM

using std::string;

only allows you to use string (without the std::). If you want to use size_type, add (I think):
using std::string::size_type;

This post has been edited by Hezekiah: 27 July 2011 - 10:40 PM

Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Character of a string | size_type Problem

Posted 27 July 2011 - 10:42 PM

Yup. Problem solved :) .
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Character of a string | size_type Problem

Posted 28 July 2011 - 08:42 AM

also, std::string contains the << operator ; therefore, you can output the string directly...if you didn't already know :)

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string str;
    cout << "Enter a line: " << endl;
    getline(cin, str);
    cout<<str;
    return 0;
}


This post has been edited by ImaSexy: 28 July 2011 - 08:42 AM

Was This Post Helpful? 1
  • +
  • -

#9 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: Character of a string | size_type Problem

Posted 28 July 2011 - 12:10 PM

ostream &operator <<(ostream &_os, const string &str)
{
_os << str.c_str();

return _os;
}

// now you can use this operator to stream 

...
string str = "....";
cout << str;


Was This Post Helpful? -2
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: Character of a string | size_type Problem

Posted 28 July 2011 - 12:19 PM

You do not need to overload operator<< for std::string, std::string already has the proper operators defined.

Jim
Was This Post Helpful? 1
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Character of a string | size_type Problem

Posted 28 July 2011 - 12:25 PM

Thanks and jimblumberg, I did not know it. Thanks !!! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1