1
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string> using namespace std;

using namespace std;

int main()
{
int spaces = 0;
string input;
cin >> input;

for (int x = 0; x < input.length(); x++) {
    if (input.substr(0, x) == " ") {
        spaces++;
    }
}
cout << spaces << endl;
system("pause");
return 0;   
}

I'm trying to make a simple program that counts the number of spaces by adding to an incrementer.

It always returns 0 for some reason.

Ozymandias
  • 156
  • 2
  • 5
  • 18
  • 1
    By the way, I do understand that you are just doing this to learn, but in "real" code, you should just use [`std::count`](http://en.cppreference.com/w/cpp/algorithm/count). Also, please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Oct 25 '16 at 08:14

1 Answers1

5

You have two problems:

  1. cin >> input; should be std::getline(std::cin, input); since std::cin will stop on the first space and not storing the rest of the string.
  2. if (input.substr(0, x) == " ") I could not understand what you meant by this expression. However, what you want is if (input[x] == ' ').

Full Code: (with minor changes)

#include <iostream>
#include <iomanip>
#include <string>     
int main(){
    unsigned int spaces = 0;
    std::string input;
    std::getline(std::cin, input);
    for (std::size_t x = 0; x < input.length(); x++) {
        if (input[x] == ' ') {
            spaces++;
        }
    }
    std::cout << spaces << std::endl;
    system("pause");
    return 0;   
}

Online Demo


As @BobTFish mentioed, the right way to do it in real code is:

#include <iostream>
#include <string>   
#include <algorithm>
int main(){
    std::string input;
    std::getline(std::cin, input);
    const auto spaces = std::count(input.cbegin(),input.cend(),' ');
    std::cout << spaces << std::endl;
    system("pause");
    return 0;   
}

Online Demo

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 1
    I would write the for loop as `for (const auto c : input) { if (c==' ') { spaces++;}}` – Martin Bonner supports Monica Oct 25 '16 at 08:52
  • @MartinBonner: Why? – Christian Hackl Oct 25 '16 at 09:02
  • @ChristianHackl Because it more precisely represents what the for loop is doing: iterating through every character in the string. The index of the string is not relevant. The fact that it is shorter is a bonus. – Martin Bonner supports Monica Oct 25 '16 at 09:05
  • @MartinBonner: It's the other way round. The `std::count` version states clearly *what* is being done, i.e. counting spaces. The `for` version represents *how* it is being done, i.e. with a conditional branch, iteration and incrementation operations. It's an implementation-centric point of view. The `std::count` version is an interface-centric point of view. – Christian Hackl Oct 25 '16 at 09:15
  • By the way, if we are talking about the "right way", then please remove the `system("pause")`. – Christian Hackl Oct 25 '16 at 09:16
  • @MartinBonner: Additionally, your version prevents `spaces` from being `const`. – Christian Hackl Oct 25 '16 at 09:16
  • I said "I would write the for-loop ...". I didn't say I would prefer to use that rather than `std::count`. – Martin Bonner supports Monica Oct 25 '16 at 09:17
  • IMO in those kind of question where the OP is asking about very basic concepts, I prefer to keep my answer simple and near to the way of the OP's used to process things. I would not be surprised if the OP asked "what is the range-for?" if I used it or even the OP may think that it would not work without a range-for. Anyway, I prefer the range-for in this case in real word code. – Humam Helfawi Oct 25 '16 at 09:31