Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new at the c++. I made basic random string program but I cant print the string to console. These are my codes :
you did not include <string> and also you take the string by copy, to edit it you'll have to pass it by reference using &
#include <string>
void randomString(std::string& line)
you don't need to assign a number to int and then assign it to char, char is an integer value in c++ so you can:
char character = rand()%122 + 97;
with this method your random numbers will not be very good, you can look into How to generate a random number in C++?
you access your string with [] operator and it has not yet been defined, to add a character to a string just use
line += character;
also if you want a random number length, there is no need for the boolean and i++ stuff:
int charNumb = rand() % 8 + 4;
while(charNumb--)
will do just fine and it looks much cleaner.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
hey guys so i am writing a program to sort a list of words alphabetically based upon my limited programming knowledge, i know there are high level functions that make it easier but i am trying to do it the hard way, anyway, i decided i would do it by adding together the alphabetical values of each character in the string and then putting them into a sorted string array based upon least to greatest value and string length, as part of this i am writing a function that will add the letters values together via a loop... here is my source
#include <iostream>
#include <string>
using namespace std;
int ABReturn_value(string a);
int main(int argc,char *argv[]){
cout<<ABReturn_value("a")<<endl;
return 0;
}
int ABReturn_value(string a){
//declare an internal character array containing the alphabet
char alphabet[2][26] = {{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}};
//variable to hold the acrued value
int value=0;
//look through the provided string's characters and acrue the value based on which address in the refernece array the letter corresponds to
for(unsigned int x = 0;x<=(a.size())-1;x++)
for(int y=0;y<=26-1;y++){
if(a[x]==alphabet[1][y]||a[x]==alphabet[2][y]){
value+=y;
}
}
return value;
}
the problem im running into is based on this test input of the character a i am expecting the function to return me a value of 1 but it is instead returning 40,ive been staring at it and cant seem to figure out what i did wrong so maybe theres something im missing? thanks in advance!
#immibis has answered this, could you tag it as answered please?
alphabet[1] should be alphabet[0] and alphabet[2] should be alphabet[1] – immibis
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find a way to convert an int to char of the corresponding value of the int (assuming int is one digit). (example 1='1' 5='5' 9='9') I've tried
int a=5;
char b=char(a+48);
whenever I try to run this the program crashes. How can I set up a system that works correctly?
It can be done using the following code:
char c = (char)(48 + a);
You can also use the '0' char value, instead of 48. It will improve code readability and let you not remember the value 48:
int a = 5;
char c = (char)((int)'0' + a);
As mentioned in comments, you can do this without explicit casts:
char c = '0' + a;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hi guys I try to print a matrix with characters.I thought like this:
#include <iostream>
using namespace std;
int main()
{
char a[3][3]={"a","b","c","d","e","f","g","h","i"};
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
}
What I doing wrong? Thank you in advance.
You have a 3 by 3 array of ints. You're trying to initialise each element of that array with string literals. There simply is no implicit conversion from the type of a string literal (const char[2] in this case) to int.
If you just want a matrix of characters, then make your array element type char. Then you need to use character literals with single quotes, instead of string literals.
char a[3][3]={'a','b','c','d','e','f','g','h','i'};
You probably want this:
char a[3][3]={'a','b','c','d','e','f','g','h','i'};
Change this:
int a[3][3]={"a","b","c","d","e","f","g","h","i"};
To:
char a[3][3]={{"a","b","c"},{"d","e","f"},{"g","h","i"}};
Try changing:
int a[3][3]={"a","b","c","d","e","f","g","h","i"};
To:
char a[3][3]={'a','b','c','d','e','f','g','h','i'};
That should get rid of casting characters to ints.
Edit: Changed the double quotations to single quotations
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals д (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm beginning in C++ and I have a simple task. As title said, I want to delete first and last character from a string for x times (where x is the lenght of the string). For example, if the string is "example", the result will be:
example
xampl
amp
m
amp
xampl
example
So far, I'm thinking like this:
#include <iostream>
#include <string>
string sir = "Example";
int len, i;
len = sir.length();
for(i=1; i<=len; i++)
{
sir.erase(sir.begin(), sir.end());
cout<<sir;
}
Or something like that... Can someone help me ?
You want to delete both the first and last char.But in the example you also added them each step. It is not actually clear what you want. Whatever you want to delete or add the characters it is feasible to keep the string unchanged. So you should use substr. Check it out here.
The problem is that
you can not use std::string::erase with integral index such as int i,
you need to use std::string::iterator
but even if you use std::string::iterator, with the current logic you would be trying to increment an iterator after the erase has been called. (such iterator is invalid)
Possible solution: assign sir.begin() to your iterator after each erase.
Here's how it could look like:
std::string sir = "Example";
for(string::iterator i = sir.begin(); i != sir.end(); i = sir.begin())
{
sir.erase(i);
std::cout << sir << std::endl;
}
outputs:
xample
ample
mple
ple
le
e
Just note that after erasing characters from your std::string, these characters are lost. You can not "restore" them. For the other half, you'll have to come with more sophisticated approach, but I'll leave that to you :)