I'm new to C++ and am having a hard time debugging this. Any idea why I am getting this error? The upper function is supposed to take in a pointer to a C-string as an argument, iterate through each character in the string and convert it to uppercase. Also, how can I get my function to return the string in uppercase?
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string upper(char* some_string){
for (int i=0; i < strlen(some_string); ++i){
toupper(some_string[i]);
cout << some_string[i];
}
return "Done"
}
int main(){
std::string word;
cout << "Enter a string: ";
cin >> word;
upper(word.c_str());
}
Make the argument const char* because the function doesn't modify the string.
Apply toupper() to what should be printed. (toupper() returns int, so the result should be casted to char to have it be printed as characters)
Add a semicolon after return "Done".
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string upper(const char* some_string){ // use const char*
for (int i=0; i < strlen(some_string); ++i){
cout << static_cast<char>(toupper(some_string[i])); // apply toupper() to what to print
}
return "Done"; // add a semicolon
}
int main(){
std::string word;
cout << "Enter a string: ";
cin >> word;
upper(word.c_str());
}
Related
The final element in the vector is the char to search for.
Here’s my code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words;
string in;
while(cin>>in)
{
words.push_back(in);
}
int size = words.size()
string check = words.at(size-1);
}
I tried your code and the loop was infinite. Try asking a set number of words then use the find method for each string. Here's an example.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> words;
string in;
cout << "Enter 5 words: \n";
for (int i{0}; i < 5; ++i)
{
cout << "Word: ";
cin >> in;
words.push_back(in);
}
cout << "What character do you want to search for? ";
char c;
cin >> c;
for (auto word : words)
{
if (word.find(c) != string::npos)
{
cout << "Character \"" << c << "\" found in " << word << endl;
}
}
return 0;
}
Edit: I noticed that you tried to use int to store the size for the string. Use size_t instead. I also think you meant to use the length method of the string, which also returns size_t.
I'm trying to convert a string of characters into their ASCII int values. However I cannot get this to work for one even one character in the string. I would expect a result of 72 when entering 'H', but it returns a 0 (the same for every character I've tried).
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
const char * b = a.c_str();
int c = atoi(b);
cout << int(c) << endl;
}
Thanks in advance.
atoi parses the C-string interpreting its content as an integral number, i.e.
int i = atoi("123"); // i = 123
You don't want this: you want to know the ASCII value of every single character of the input string. To figure this out, you can use this code snippet:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for(int c: a)
cout << c << '\n';
}
I'm not quite used to the string library but simply type:
cout<<(int)a[pozition];
You can place that in a for like this.
for(int i=0;i<a.length();i++)
cout<<(int)a[i]<<endl;
You can just cast each character to an int
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.length(); i++) {
cout << (int) a[i] << endl;
}
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
main()
{
bool string1[20];
cout << "Enter string: ";
cin >> string1;
int counter = 0;
int length;
length = strlen(string1);
This is incomplete code, but my question is why am I getting a compiling error when using cin? It says:
error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘bool [20]’)
On this line:
cin >> string1;
I'm not sure how to fix this.
bool string1[20]; is the wrong choice for the user input as a string, all it does is create an array of 20booleans, true or false which is not what you want.
what you are after is your included #include <string>
string string1;
cout << "Enter string: ";
cin >> string1;
Instead of using strlen you get the length by using the length method provided by std::string
auto length = string1.length()
There is no operator>> for reading an array of bool values. What you need is an array of char values instead:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
char string1[20];
cout << "Enter string: ";
cin >> setw(20) >> string1;
int length = strlen(string1);
Or better, a single std::string:
#include <iostream>
#include <string>
using namespace std;
int main() {
string string1;
cout << "Enter string: ";
cin >> string1;
int length = string1.length();
I think you are confusing string with array. string1 in your code is not an string its an array. So, you can't just put data in it without giving the proper index number. Also remember its an bool type so you can only enter 0/1/true/false value.
Again, you have used strlen() function in your code which is used for determining the length of the string but your is an array. You didn't ask about this but when I ran the code in my IDE it got error.
Here is one way to do it :
main()
{
bool string1[20];
cout << "Enter string: ";
for(int i=0;i<20;i++)//iterating through the boolian array
{
cin >> string1[i];
}
int counter = 0;
int length;
length = sizeof(string1)/sizeof(string1[0]);
cout<<length;//printing the size of the array
}
I need a help in a very basic c++ code.
My program is about guessing name game the problem which i faced is in reading string char by char
#include <iostream>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
void Play(int, int,int, string[], string[]);
string GetRandomName(int, int, int , string[], string[]);
const int ArrayMax = 100;
void Play(int selection, int FArraySize, int MArraySize,string Female[], string Male[])
{
int MAX_TRIES = 3;
int i=0;
ofstream ofFile;
ifstream InFile;
int num_of_wrong_guesses=0;
char letter;
string GuessedName;
GuessedName = GetRandomName(selection, FArraySize, MArraySize, Female, Male);
cout << "Guess the following name:" << endl;
while (GuessedName[i]!= 0 ){
cout<<"?";
i++;
}
cout << "\nEnter a guess letter? or * to enter the entire name" << endl;
cin >> letter;
return;
}
I don't complete coding...
the problem is in the while loop how can i solve it without using cstring?
could you help me?
int i = 0;
while(GuessedName[i] != 0)
{
cout << "?";
i++;
}
Seems like you are trying to print sequence of ? with the length of the string to guess. But you cannot treat std::string as c-string. When its length is n, GuessedName[n] is string subscript out of range - you cannot access one element past end - it's not null-terminated. Use for loop:
for(int i = 0; i < GuessedName.length(); ++i)
cout << "?";
Or simply:
cout << std::string(GuessedName.length(), '?');
Change the while loop like this:
while (GuessedName[i]){
cout<<"?";
i++;
}
I have a string of digits. I am trying to print it as an int type each single digit in the string using istringstream. It works fine if pass whole string as argument to conversion function in main but if I pass it by index, it raises error.
How to make this code work using index to print each single digit in string array as an int.
Here is my code.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int strToNum(string str)
{
istringstream ss(str);
int n;
ss>>n;
cout<<n;
}
int main()
{
string str = "123";
for(int i=0; i<str.length(); i++)
//strToNum(str); Works fine
strToNum(str[i]); //raises error
}
str[i] is a char, while the strToNum expects a string, hence the type error.
It raises error because str[i] is a char
however , strToNum(string str) excepts a string
Try this :
for(int i=0; i<str.length(); i++)
strToNum(string(1,str[i])); //Convert char to string
See here
Others have explained your error. This is how you could make it work:
strToNum( std::string(1, str[i]) );
But I'd do this instead:
for(int i=0; i<str.length(); i++)
cout << str[i] - '0';
But ask yourself if you really need this. Are you interested in the value or the representation? If the latter, just print chars.
You don't need istringstream at all.
int strToNum(char ch)
{
cout << ch;
}
Actually I use a template function to perform this task, which is a more useful way to write the function that originated this thread ( because this single function can convert a string to any type of number: int, float, double, long double ):
#include "stdafx.h"
#include <string>
#include <iostream>
#include <Windows.h>
#include <sstream>
#include <iomanip>
using namespace std;
template <typename T>
inline bool StrToNum(const std::string& sString, T &tX)
{
std::istringstream iStream(sString);
return (iStream >> tX) ? true : false;
}
void main()
{
string a="1.23456789";
double b;
bool done = StrToNum(a,b);
cout << a << endl;
cout << setprecision(10) << b << endl;
system ("pause");
}
setprecision(10) ( iomanip ) is required otherwise istringstream will hide some decimals