#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.
You have two problems:
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.
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
Related
I have a my code that's look like this
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x;
char chars = '*';
cin >> x;
for (int i=1; i<=x; i++){
cout << chars * i << endl;
}
cout << "\n\n";
system("pause");
return 0;
}
and it compiled succesfully but, when I run it i just display this
1
42
and I want to print ('*') in x times, please someone help me
To do what you want you can do this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x;
char chars = '*';
cin >> x;
for (int i=1; i<=x; i++){
cout << chars;
}
cout << "\n\n";
system("pause");
return 0;
}
As stated in comments - char when multiplied by int results in int.
Is below code what you wanted?
#include <iostream>
int main()
{
int x;
char const chars = '*';
std::cin >> x;
for (int i=1; i<=x; i++) std::cout << chars << std::endl;
return 0;
}
I'm new to C++ programming. Whenever I run this, it doesn't print anything.
What I want is to ask the user for their names infinite times and only break after typing "quit". And after typing "quit" it must print all the items in the array. But it's not printing anything. Where am I wrong?
Here's my code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int count = 0;
std::string arr[] = {};
void my_funct()
{
for (int i = 0; i < count; i++){
std::cout << arr[i];
}
}
int main()
{
std::string names;
while (true)
{
std::cout << "Enter your name: \n";
getline(std::cin, names);
if (names == "quit")
{
my_funct();
break;
}
else
{
std::string arr[] = {names};
count++;
}
}
}
arr in my_funct() and arr in if-else statement are two different arrays. You are declaring local version in if-else, which after each iteration of loop is destroyed. You also declared it as an array of zero length - meaning it cannot hold any elements. What you want in your case is std::vector (you even included appropriate header).
You can then resign from count variable and use vec.size() (I've renamed arr to vec) instead.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
std::vector<std::string> vec;
void my_funct()
{
for (int i = 0; i < vec.size(); ++i) {
std::cout << vec[i];
}
}
int main()
{
std::string name;
while (true) {
std::cout << "Enter your name: \n";
getline(std::cin, name);
if (name == "quit") {
my_funct();
break;
}
else {
vec.push_bash(name);
}
}
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int count = 0;
vector<string> arr;
void my_funct()
{
for (auto ar : arr) {
std::cout << ar << endl;
}
}
int main()
{
std::string names;
while (true)
{
std::cout << "Enter your name: \n";
getline(std::cin, names);
if (names == "quit")
{
my_funct();
break;
}
arr.push_back(names);
}
}
There is your code I tried to correct. If you need do not understand or need explanation just ask and I'll try to explain
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int counet = 0;
vector<string> arr;
void my_funct()
{
for (int i = 0; i < arr.size(); i++){
cout << arr[i] << endl;
}
}
int main()
{
std::string names;
while (true)
{
std::cout << "Enter your name: \n";
getline(std::cin, names);
if (names == "quit")
{
my_funct();
break;
}
else
{
arr.push_back(names);
}
}
}
Hello how to get all strings until find space and push_back the words until space in the second turn of For loop to start getting all string after space and again until find space that's my code
for example this sentece 5bbbb3 1f a0aaa f1fg3
i want to get bbbb and push_back into in a vector of chars then to push_back aaaa and so
vector of chars vec = vec.[0] == 'bbbb' vec.[1] == 'aaaa' vec.[2] == 'f' vec.[3] == 'ffg'
Thank you in advanced
these are my 2 codes both does not work
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string sentece;
getline(cin, sentece);
vector<char> words;
for (int i = 0; i < sentece.size(); ++i)
{
while (sentece.substr(i, ' '))
{
if(isalpha(sentece.at(i)))
{
words.push_back(sentece.at(i));
}
}
}
cout << words[0] << '\n';
cout << words[1] << '\n';
cout << words[2] << '\n';
for(const auto& a : words)
{
cout << a;
}
return 0;
}
//==================================================================
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string sentece;
getline(cin, sentece);
vector<char> words;
for (int i = 0; i < sentece.size(); ++i)
{
while (sentece.at(i) != ' ')
{
if(isalpha(sentece.at(i)))
{
words.push_back(sentece.at(i));
}
if(sentece.at(i) == ' ')
{
break;
}
}
}
cout << words[0] << '\n';
cout << words[1] << '\n';
cout << words[2] << '\n';
for(const auto& a : words)
{
cout << a;
}
return 0;
}
I believe this code should give you the answer you want:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string test = "5bbbb3 1f a0aaa f1fg3";
vector<string> words;
for (int i = 0; i < test.size(); i++)
{
string tmp = "";
while (test[i] != ' ' && i < test.size())
{
if (isalpha(test.at(i))){
tmp += test[i];
}
else if (test.at(i) == ' ')
{
i++;
break;
}
i++;
}
words.push_back(tmp);
}
cout << words[0] << '\n';
cout << words[1] << '\n';
cout << words[2] << '\n';
cout << words[3] << '\n';
}
All you have to do is then replace the test sentence with your user input. You were forgetting to increment i in the while loop so it was examining the same character every time and getting stuck infinitely.
I tried to use as much of your original code as possible so don't assume that this is the most efficient or elegant solution to the problem
Hope this helps :)
You can use character array with scanf()
`
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[1000];
scanf("%[^' ']%s", s);
cout<<s;
}
This will stop taking input untill you hit enter but this will store string upto only till first occurence of space.
I am a beginner and taking a CSC course, I have to write a program that converts a user input string into the sum of the ASCII value of each character, here is what I have so far, and Im still pretty far from being done. But any help would be greatly appreciated. Thanks
#include <iostream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
using std::string;
using std::cout;
using std::endl;
int main()
{
{
int x;
std::cout << "enter string" << std::endl;
std::cin >> x;
}
string text = "STRING";
for (int i = 0; i < text.size(); i++)
cout << (int)text[i] << endl;
return 0;
}
You can use a range-based for loop to traverse the string and then add up each char within:
#include <iostream>
#include <string>
int main()
{
int sum = 0; // this is where all the values are being added to
std::string s;
std::cout << "enter string and press enter." << std::endl;
std::cin >> s; // string that the user enters will be stored in s
for (char c : s)
sum += c;
std::cout << "total ASCII values: " << sum << std::endl;
return 0;
}
I'm new to c++, what i'm trying to do is count the amount of times a letter occurs with a piece of text or paragraph and stores this into an array called frequency array.
The code below is working to a degree, what happens is if the user types hello frequencyarray stores 11121, if the user types aaba frequency array stores 1213
I don't want a running total i'm wanting the array to store 1121 and 31. so if the same letter appears it adds 1 to the array.
Thanks David
#include <iostream> //for cout cin
#include <string> //for strings
#include <fstream> //for files
using namespace std;
int main()
{
string text;
int frequencyarray [26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter Word: ";
cin >> text;
//***************************COUNT OCCURANCES************************
for (int i(0); i < text.length(); i++)
{
char c = text[i];
c = toupper(c);
c -= 65;
if (c < 26 || c >=0)
{
frequencyarray[c]++;
cout << frequencyarray[c];
}
}
system ("pause");
return(0);
}`
If you don't want the running total, don't have the cout << freqencyarray[c]; inside the cycle that is counting the occurrences.
Try this
#include <iostream> //for cout cin
#include <string> //for strings
#include <fstream> //for files
#include <algorithm>
using namespace std;
int main()
{
string text;
int frequencyarray [26]={0};
cout << "Enter Word: ";
cin >> text;
//***************************COUNT OCCURANCES************************
for (int i(0); i < text.length(); i++)
{
char c = text[i];
c = toupper(c);
c -= 65;
if (c < 26 || c >=0)
{
frequencyarray[c]++;
}
}
std::for_each(std::begin(frequencyarray), std::end(frequencyarray), [](int i)
{
std::cout << i << ",";
});
std::cout << "\n";
system ("pause");
return(0);
}