• A Great friend to the HardForum with a great kid that he is trying to get a scholorship to continue his schooling. Please give hime a vote! Only 24 hours left! Thanks.
    If you have an VOTE FOR KEENAN!

Really simple(strings and spaces in C++)

Grazehell

2[H]4U
Joined
Jun 13, 2001
Messages
2,660
I want to enter a string with spaces and want the program to tell me what is size of the entire thing, not just up to the first space.

Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;




int main()

{

	string alpha;
	
	int alpha_len;

cout <<"Enter String with a space in between" <<endl;

cin>> alpha;

alpha_len = alpha.size();


cout<<"String lenght is "<<alpha_len<<endl;
 
system("PAUSE");

  
  return 0;
      }
 
Grazehell, since you're doing this as an academic excercise, I would recomment looking up information about buffer overflows and cin >>.
 
Grazehell, since you're doing this as an academic excercise, I would recomment looking up information about buffer overflows and cin >>.
As I understand it, buffer overflows would be applicable to character arrays but not to STL strings (which the OP uses).
 
yes, you are correct. as stated, it was for an academic exercise. my bet is that if he didn't know how to use cin.get() that also wasn't aware of the buffer overflow problem, which he's bound to encounter sometime.
 
yes, you are correct. as stated, it was for an academic exercise. my bet is that if he didn't know how to use cin.get() that also wasn't aware of the buffer overflow problem, which he's bound to encounter sometime.

I know about cin.get and it did not work here!
I accomplished what I set out to do, I am just working on a decent enough algorithm which is teaching me how to do exponentials and roots in C++
 
As someone who doesn't know much C++ I can only recommend that there is a lot more to be gained by learning what the different libraries do than by reinventing the wheel. cmath will do exponential and roots
 
As someone who doesn't know much C++ I can only recommend that there is a lot more to be gained by learning what the different libraries do than by reinventing the wheel. cmath will do exponential and roots

That's what I am using. But for some reason I am catching my butt.It would not necessarily be programming related problems but me trying to sort out my algorithm.
 
Back
Top