0

I am making a very simple programs just trying to learn c++ as a beginner. It is designed to simply count the number of vowells and the number of consenants in a short sentence. I have come across an interesting little dilema while doing it.

First of all here is the code:

    #include <iostream>
    #include <time.h>
    #include <string>


    using namespace std;



    int main()
   {    int vowells = 0, consenants = 0;
char sentence[100];
char alphabet[] = {'a','e','i','o','u','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
cout << "Please type in a sentence (up to 100 characters max, lower case only): ";

cin >> sentence;
cin.ignore();

cout << "Your sentence was: " << sentence;

for (int i=0; i < sizeof(sentence); i++)
{
    if (sentence[i] == alphabet[0]||sentence[i] == alphabet[1]||sentence[i] == alphabet[2]||sentence[i] == alphabet[3]||sentence[i] == alphabet[4])
    {vowells++;}
    else if (sentence[i] == alphabet[5]||sentence[i] == alphabet[6]||sentence[i] == alphabet[7]||sentence[i] == alphabet[8]||sentence[i] == alphabet[9]||sentence[i] == alphabet[10]||sentence[i] == alphabet[11]||
        sentence[i] == alphabet[12]||sentence[i] == alphabet[13]||sentence[i] == alphabet[14]||sentence[i] == alphabet[15]||sentence[i] == alphabet[16]||sentence[i] == alphabet[17]||sentence[i] == alphabet[18]||
        sentence[19] == alphabet[20]||sentence[i] == alphabet[21]||sentence[i] == alphabet[22]||sentence[23] == alphabet[24]||sentence[i] == alphabet[25])
    {consenants++;}
}

cout << "\nThe number of vowells is: " << vowells;
cout << "\nThe number of consenants is: " << consenants;

cin.get();


    }

Sorry it looks really messy. Basically after the cin >> sentence; line i put the cin.ignore() function to get rid of the enter key pressed after inputing the sentence. At the end of the function the cin.get() is simply suppose to serve as a breaking point so that the program wants another input before it closes. If i input just 1 word with no spaces, the program runs and pauses at the end as desired. If i put in multiple words with spaces, it just runs and closes immediately without giving me time to even see it. I assume this is because of the spaces for some reason... Though i'm not sure why they would affect it in this way.

So basically, is it the spaces that are giving me my problems? If so how do i go about either getting rid of them or at least having the program ignore them?

Thanks!

EDIT*** So i was told that i could use the windows Sleep() command to get it to pause, and that worked. Now the problem is that as another person commented, the cin function only accepts the first word, and doesn't take into account the rest of the sentence. So i guess i need to get rid of the spaces or somehow use a different input function to get it to work properly. Any suggestions on how to go about doing this?

Riley
  • 391
  • 1
  • 2
  • 11

2 Answers2

1

From what I know, there is no standard, cross-platform way to "Sleep" in C++. If you're running Windows, you can use the following:

#include <windows.h>
int main(){
    //do stuff
    Sleep(1000); // this will "sleep" for 1s (1000ms)
}

If you aren't using Windows, I'm sure there are Linux/etc alternatives out there as well to do something similar. I've seen your way used before, but if it isn't working, this should be a nice alternative. You could use threads, for example. That could work.

Community
  • 1
  • 1
Ricky Mutschlechner
  • 4,291
  • 2
  • 31
  • 36
  • 4
    There is [`std::this_thread::sleep_for( time_duration );`](http://en.cppreference.com/w/cpp/thread/sleep_for). – juanchopanza Aug 05 '13 at 16:16
  • 1
    Ok thank you Ricky, doing what you said makes my program stay open long enough to see it, which actually brings up another question which i am going to add to my original post in an edit. – Riley Aug 05 '13 at 16:18
  • 1
    We sent men to the moon over 40 years ago, it shouldn't be that difficult to get a Windows IDE to leave a console window open until *you* want to close it, rather than when *it* does. Opening up a console window and running your program manually is better than polluting your code with this kind of eye-watering stuff. – Crowman Aug 05 '13 at 17:16
  • @PaulGriffiths although I completely agree with the first part of your statement, the second, not so much - in one aspect. Two lines of code isn't THAT much pollution, but I do see your point. It should certainly be easier than std::this_thread::sleep_for(time); to pause a console window, that's for sure. – Ricky Mutschlechner Aug 05 '13 at 17:44
1

Yes, the spaces are giving you problems. >> stops extracting characters into the buffer when a whitespace character is encountered. So if you have one word followed by a space then another word, only the first word is placed in the buffer with a single call to >>. Your later call to cin.get already has a character to read form the stream since they were not all taken by the >> call.

getline provides a way to extract whitespace as well as other characters.

  //cin >> sentence;
  //cin.ignore();
  cin.getline(sentence, 100)
A.E. Drew
  • 2,097
  • 1
  • 16
  • 24
  • Excellent, i had the syntax for getline() wrong before when i tried it. Thank you that works great. – Riley Aug 05 '13 at 16:33