View previous topic :: View next topic |
Author |
Message |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Sep 06, 2009 6:57 pm Post subject: [C++]Creating arrays/strings questions. |
|
|
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 |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sun Sep 06, 2009 7:18 pm Post subject: |
|
|
You can use cin.get to get spaces.
Why don't you use an actual char array instead of string?
|
|
Back to top |
|
 |
Guy Expert Cheater
Reputation: 0
Joined: 30 May 2009 Posts: 187
|
Posted: Sun Sep 06, 2009 7:39 pm Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Sep 06, 2009 8:33 pm Post subject: |
|
|
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 |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Sep 06, 2009 10:54 pm Post subject: |
|
|
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 |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sun Sep 06, 2009 11:10 pm Post subject: |
|
|
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 |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Sun Sep 06, 2009 11:37 pm Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Sep 06, 2009 11:39 pm Post subject: |
|
|
was my reply completely ignored or what.
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Sep 07, 2009 2:38 am Post subject: Re: [C++]Creating arrays/strings questions. |
|
|
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 |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Mon Sep 07, 2009 10:16 am Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Sep 07, 2009 5:01 pm Post subject: |
|
|
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 |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Mon Sep 07, 2009 6:35 pm Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
Back to top |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Tue Sep 08, 2009 12:23 am Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Sep 08, 2009 12:54 am Post subject: |
|
|
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 |
|
 |
|