C
C
Chipu2018-09-06 07:22:49
C++ / C#
Chipu, 2018-09-06 07:22:49

Error while executing the program, how to fix it?

// ConsoleApplication3.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
  string s;
  cin >> s;
  int probel = 0;
  //Перед или после запятой пробел не ставится.
  for (int i = 0; i < sizeof(s); i++) 
  {
    if (s[i]==' ')
    {
      probel++;
    }
  }
  cout << probel<<"\t"<<sizeof(s);
  system("Pause");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniel Demidko, 2018-09-06
@Chipu

You have an error, it reads characters into a string up to the first space. To read the entire string, including spaces, you need to use your second mistake in that it returns not the length of the string, but the size of the type . To get the length of a particular string, you need to use or And, finally, you don't need a string. Delete it. std::cin>>s
#include <cstring>

D
devalone, 2018-09-06
@devalone

sizeof(s)

if you need the size of the string, then s.size()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question