Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]Creating arrays/strings questions.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Sun Sep 06, 2009 6:57 pm    Post subject: [C++]Creating arrays/strings questions. Reply with quote

Hello, for learning purposes, I am making a small win32 console program that asks you to enter a sentence, then asks you to pick a letter, and then counts how many letters are in that sentence and returns the value.

Here is a snippet of my code:

Code:
string enter;
   string letter;
   cout << "Enter a sentence: " << endl;
   cin >> enter;
   cout << "Pick a letter: " << endl;
   cin >> letter;


Question 1:

How can I make it so that when I have a space in my sentence, it won't skip the "Pick a letter" part?

That's confusing, but let me put it in an example.

Lets say I type "Hello, this is my sentence." Obviously, you can see that there are spaces in my sentence. But when there are spaces, it skips the "pick a letter" part. If I did "hellothisismysentence" in the "Enter your sentence" part, It would let me choose a letter.


Question 2:
How would I go about putting my sentence to an array, then counting how many of the letters I picked?
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Sun Sep 06, 2009 7:18 pm    Post subject: Reply with quote

You can use cin.get to get spaces.

Why don't you use an actual char array instead of string?
Back to top
View user's profile Send private message MSN Messenger
Guy
Expert Cheater
Reputation: 0

Joined: 30 May 2009
Posts: 187

PostPosted: Sun Sep 06, 2009 7:39 pm    Post subject: Reply with quote

Noz3001 wrote:
You can use cin.get to get spaces.

Why don't you use an actual char array instead of string?


Because STL is soooo much cooler!

But seriously, OP, use linear types (As suggested, a char array, or a char array pointing to a memory heap, [...] would fit better).

If not, you can always convert to a char array using the string function member c_str.

_________________
Has anyone seen Hitler around..? If so, PM me!
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Sep 06, 2009 8:33 pm    Post subject: Reply with quote

Noz3001 wrote:
You can use cin.get to get spaces.

Why don't you use an actual char array instead of string?


because std::string is superior in many ways?


@op:

you want
Code:
std::getline(std::cin, string);
Back to top
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Sun Sep 06, 2009 10:54 pm    Post subject: Reply with quote

Noz3001 wrote:
You can use cin.get to get spaces.

Why don't you use an actual char array instead of string?
That's the thing, I'm trying to learn how to use arrays, strings and etc. I'm a complete newbie to C++, and can't understand anything besides the basic program "Hello World". I'm trying to further my knowledge on this language.
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sun Sep 06, 2009 11:10 pm    Post subject: Reply with quote

Have you read any tutorials/guides yet?

I would recommend these two:

cprogramming.com

cplusplus.com

both of these sights have great beginners tutorials, but I dont feel like finding the direct links.

_________________
Back to top
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Sun Sep 06, 2009 11:37 pm    Post subject: Reply with quote

manc wrote:
Have you read any tutorials/guides yet?

I would recommend these two:

cprogramming.com

cplusplus.com

both of these sights have great beginners tutorials, but I dont feel like finding the direct links.

Ah, thank you. I will look into those videos tomorrow morning. (It's pretty late here) I was mostly using antiRTFM's guides on youtube, as they are pretty newbie spoon fed.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Sep 06, 2009 11:39 pm    Post subject: Reply with quote

was my reply completely ignored or what.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Mon Sep 07, 2009 2:38 am    Post subject: Re: [C++]Creating arrays/strings questions. Reply with quote

slovach answered the first.. Here's the second:

CrazyBob wrote:
Question 2:
How would I go about putting my sentence to an array, then counting how many of the letters I picked?
Code:
std::string s;
std::getline( std::cin, s );
std::stringstream ss( s );
char *buf = new char [ s.length()+1 ];
ss.getline( buf, s.length()+1 );
delete [] buf;
Or you just could use s.c_str()..
Back to top
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Mon Sep 07, 2009 10:16 am    Post subject: Reply with quote

slovach wrote:
was my reply completely ignored or what.
No, no, sorry slovach! Right now I'm trying to figure out how to put that into my program.

@slovach, with
Code:
std::getline(std::cin, string);

I get:

Quote:
c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string'


@Jani, with
Code:
std::string s;
std::getline( std::cin, s );
std::stringstream ss( s );
char *buf = new char [ s.length()+1 ];
ss.getline( buf, s.length()+1 );
delete [] buf;


I get
Quote:
c:\documents and settings\owner\my documents\visual studio 2008\projects\tutorial1\tutorial1\tutorial1.cpp(21) : error C2228: left of '.getline' must have class/struct/union
1> type is 'int'


I'm using Visual C++ 2008. Sorry if I sound repetitive, I'm completely new to this.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Sep 07, 2009 5:01 pm    Post subject: Reply with quote

Code:
#include <iostream>
#include <string>

int main()
{
   std::string str;
   std::cout << "Darling, won't you type me a sentence with lots of spaces?" << std::endl;
   std::getline(std::cin, str);
   std::cout << str;
   
   return 0;
}
Back to top
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Mon Sep 07, 2009 6:35 pm    Post subject: Reply with quote

Okay, this is what I have so far. I commented out everything.

Code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include "stdlib.h"   //includes
using namespace std;

int main()
{
   string enter; //creates string enter
   string str; //creates string str
   char letter[256]; //creates an array for 256 letters
   cout << "Enter a sentence:" << endl; //promts you to enter a sentence
    getline(cin, str); //"gets" what the user puts in and puts it in str
   cout << "This is your sentence: ";
   cout << str << endl; //repeats what your sentence is
   cout << "Pick a letter: " << endl; //promts you to enter a letter
   cin.getline (letter,256); //puts your letter into char letter[256]

   system("PAUSE");//press any key to continue...
   return 0;
}


After it puts the letter into the array, how do I "scan" so to speak for the letter I entered?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Sep 07, 2009 8:32 pm    Post subject: Reply with quote

str.find

http://www.cplusplus.com/reference/string/string/
Back to top
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Tue Sep 08, 2009 12:23 am    Post subject: Reply with quote

slovach wrote:
str.find

http://www.cplusplus.com/reference/string/string/

Thanks, slovach, you help a bunch! But unfortunately, I'm still not getting the returned result I'm looking for.

Code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include "stdlib.h"   //includes
using namespace std;

int main()
{
   string enter; //creates string enter
   string str; //creates string str
   string letter; //creates an array for 256 letters
   char found; //
   cout << "Enter a sentence:" << endl; //promts you to enter a sentence
    getline(cin, str); //"gets" what the user puts in and puts it in str
   cout << "This is your sentence: ";
   cout << str << endl; //repeats what your sentence is
   cout << "Pick a letter: " << endl; //promts you to enter a letter
   getline (cin, letter); //puts your letter into the string letter.
   cout << "You picked the letter: "<< letter << endl; // tells you what letter you picked.
   found = str.find(letter) + 1; // finds what is in the string letter
   (found!=string::npos) //I'm not sure what this does, this was copy paste. My guess: if it isn't the letter, don't count it.
   cout << "Found: " << int(found) << endl; // prints how many found.
   system("PAUSE");//press any key to continue...
   return 0;
}


I'm getting weird results..
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Tue Sep 08, 2009 12:54 am    Post subject: Reply with quote

Does that really compile for you? You're missing an if statement.


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

int main()
{
   string   str_base,   //base string
         str_search; //and string to search for in it

   cout << "Enter a sentence: " << endl;
   getline(cin, str_base);

   cout << "This is your sentence: ";
   cout << str_base << endl;

   cout << "Enter what to search: " << endl;
   getline (cin, str_search);
   cout << "Searching for: " << str_search << endl;

   int count   = 0;
   int pos      = 0;
   while(pos != string::npos)
   {
      pos = str_base.find(str_search, pos);
      if(pos != string::npos)
      {         
         pos += str_search.length();
         count++;
      }
   }

   cout << str_search << " occurs " << count << " times" << endl;
   return 0;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites