My professor is having us make our own version of the functions mystrcat, mystrlen and mystrcopy. I have a problem where the word is returning the wrong amount of bytes. Or, rather, I think it is.
The phrase: "HELLO WORLD!" , if I am not wrong, should return 13 bytes? Like in:
const char char_literal[] =
{ 'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!', '\0' };
// 1 2 3 4 5 6 7 8 9 10 11 12 13
I wrote the function myStrLen() that returns ({ 'H', 'E', 'L', 'L', 'O', ' '}) as 6, but because it is alone, there should be a '\0' at the end right?
So should I be returning i + 1 or is 6 correct?
int myStrLen(char stringInput[]){
for(int i = 0; ; i++){
if(stringInput[i] == '\0'){
return i;
If your professor tells you to write a function that behaves exactly as strlen, then it shall not count the string termination character:
std::size_t strlen( const char* str );
Returns the length of the given byte string, that is, the number of
characters in a character array whose first element is pointed to by
str up to and not including the first null character. ...
So returning i is correct, besides the fact that the "original" strlen takes a const input parameter and returns a size_t...
if (selecao > 0 && selecao < 5) {
if (assentosP[selecao] == 'O') {
printf("Test");
assentosP is a char array, that has a bunch of either D or O letters (D by default)
char assentosP[2][4] = {
{'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]...
{'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]...
};
What I need to know is a way to make AssentosP find out if the char in position selecao of the array is either D or O.
When I try it, it gives me a error
iso c++ forbids comparison between pointer and integer c
You defined a two dimensional array
char assentosP[2][4] = {
{'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]...
{'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]...
};
Thus expression assentosP[selecao] has type char[4]. You may not compare such an array with a character (or an integer after the integer promotion) like this
if (assentosP[selecao] == 'O') {
Take into account that if an array declared as having 4 elements then the valid range of indices is [0, 3]. Thus this statement
if (selecao > 0 && selecao < 5) {
looks incorrectly.
I'm making a card shuffling function for the card game. I created an array of Card objects. Then I tried to rearrange the objects in the array using random_shuffle. But it doesn't work.
char faces[13] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
char suits[4] = { char(3), char(4), char(5), char(6) };
int values[13] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
Card** deck = new Card*[52];
for (int row = 0; row <= 3; row++)
{
for (int column = 0; column <= 12; column++)
{
deck[Card::getCounter()] = new Card(suits[row], faces[column], values[column], true);
}
}
int size = sizeof(deck) / sizeof(deck[0]);
random_shuffle(*deck, *deck + size);
I mean, if I check with cout, like
cout << deck[0]->getFace()<< deck[0]->getSuit() << endl;
it shows 2(heart), like it was before using random_shuffle
The problem with your code is that operator size returns the size of a pointer (8 on a 64-bit machine) rather than the size of the array it points to. As a consequence, the expression
sizeof(deck) / sizeof(deck[0])
returns 1, and you only shuffle a single value, which means you don't shuffle.
The solution can be:
use the explicit size of the array
random_shuffle(*deck, *deck + 52);
Better, define
const int NUM_OF_CARD_IN_DECK= 52
and use it anywhere you need it
Better still, use an std::vector
Hello I'm trying to print const * char string ended with '\0'
But when I rying to use cout
const char command[9] = {'S', 'T', 'E', 11, 12, 13, 14, 15, '\0'};
const char * i = command;
cout<<i;
It give me some squares "[][][]"
So I tried to iterate whole chars
while(*i != '\0'){
cout << *i;
i++;
}
It give me : S[][][]. Progress?
When I print *i it give me 'S' but *(i+1) '[][]'.
Is problem with this that I had 8 bit chars, and int is 32 bits?
Or if I doing something wrong?
Solved:
char 13 as a "vertical tab" retur carrage to the start and hide from console "STE", and then C++ print 3 other non printable values.
-Now I need a coffie..
This is the problem
const char command[9] = {'S', 'T', 'E', 11, 12, 13, 14, 15, '\0'};
since the ascii values 11, 12, 13, 14, 15 are not printable characters, try this
const char command[14] = {'S', 'T', 'E', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '\0'};
Image from http://www.asciitable.com/
Solved:
char 13 as a "vertical tab" retur carrage to the start and hide from console "STE", and then C++ print 3 other non printable values. -Now I need a coffie..
Sorry I stuck with this.
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 very new and horrible at C++.
I was hoping someone could figure out what this error means, I seriously have no idea what to do, I am NOT asking to have you to try to make it more effiecent, I realize my uneffieceny.
I am trying to convert letters to numbers, I understand this is weak encryption.
Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
std::string ptxt = "";
string etxt = "";
cin >> ptxt;
std::replace( ptxt.begin(), ptxt.end(), 'a', '1');
std::replace( ptxt.begin(), ptxt.end(), 'b', '2');
std::replace( ptxt.begin(), ptxt.end(), 'c', '3');
std::replace( ptxt.begin(), ptxt.end(), 'd', '4');
std::replace( ptxt.begin(), ptxt.end(), 'e', '5');
std::replace( ptxt.begin(), ptxt.end(), 'f', '6');
std::replace( ptxt.begin(), ptxt.end(), 'g', '7');
std::replace( ptxt.begin(), ptxt.end(), 'h', '8');
std::replace( ptxt.begin(), ptxt.end(), 'i', '9');
std::replace( ptxt.begin(), ptxt.end(), 'j', '10');
std::replace( ptxt.begin(), ptxt.end(), 'k', '11');
std::replace( ptxt.begin(), ptxt.end(), 'l', '12');
std::replace( ptxt.begin(), ptxt.end(), 'm', '13');
std::replace( ptxt.begin(), ptxt.end(), 'n', '14');
std::replace( ptxt.begin(), ptxt.end(), 'o', '15');
std::replace( ptxt.begin(), ptxt.end(), 'p', '16');
std::replace( ptxt.begin(), ptxt.end(), 'q', '17');
std::replace( ptxt.begin(), ptxt.end(), 'r', '18');
std::replace( ptxt.begin(), ptxt.end(), 's', '19');
std::replace( ptxt.begin(), ptxt.end(), 't', '20');
std::replace( ptxt.begin(), ptxt.end(), 'u', '21');
std::replace( ptxt.begin(), ptxt.end(), 'v', '22');
std::replace( ptxt.begin(), ptxt.end(), 'w', '23');
std::replace( ptxt.begin(), ptxt.end(), 'x', '24');
std::replace( ptxt.begin(), ptxt.end(), 'y', '25');
std::replace( ptxt.begin(), ptxt.end(), 'z', '26');
return 0;
}
Build Log (Debug?)
Let's start from the beginning of the build log:
main.cpp|25|warning: multi-character character constant [-Wmultichar]|
A character constant is like 'a' or '1'. If we go and look at the code this warning points to we see:
std::replace( ptxt.begin(), ptxt.end(), 'l', '12');
Note that '12': there are two characters inside that character constant. This is warning you about the fact that you have more than one. What exactly a 'multi-character' character constant is and what they're good for doesn't matter, you don't need them and should not be using them.
If you really want text like "hello" to be encoded as "85121215" or "8 5 12 12 15" then you're going to have to rethink how you do the replacement. This is for two reasons: a multi-character character constant like '12' will not look like 12 in your output file, and secondly because std::replace will not handle replacing a single character like l with multiple characters.
For now lets replace all the mult-character character constants you've got with single-character versions:
std::replace( ptxt.begin(), ptxt.end(), 'a', '1');
std::replace( ptxt.begin(), ptxt.end(), 'b', '2');
std::replace( ptxt.begin(), ptxt.end(), 'c', '3');
std::replace( ptxt.begin(), ptxt.end(), 'd', '4');
std::replace( ptxt.begin(), ptxt.end(), 'e', '5');
std::replace( ptxt.begin(), ptxt.end(), 'f', '6');
std::replace( ptxt.begin(), ptxt.end(), 'g', '7');
std::replace( ptxt.begin(), ptxt.end(), 'h', '8');
std::replace( ptxt.begin(), ptxt.end(), 'i', '9');
std::replace(ptxt.begin(), ptxt.end(), 'j', 'a');
std::replace(ptxt.begin(), ptxt.end(), 'k', 'b');
std::replace(ptxt.begin(), ptxt.end(), 'l', 'c');
std::replace(ptxt.begin(), ptxt.end(), 'm', 'd');
std::replace(ptxt.begin(), ptxt.end(), 'n', 'e');
std::replace(ptxt.begin(), ptxt.end(), 'o', 'f');
std::replace(ptxt.begin(), ptxt.end(), 'p', 'g');
std::replace(ptxt.begin(), ptxt.end(), 'q', 'h');
std::replace(ptxt.begin(), ptxt.end(), 'r', 'i');
std::replace(ptxt.begin(), ptxt.end(), 's', 'j');
std::replace(ptxt.begin(), ptxt.end(), 't', 'k');
std::replace(ptxt.begin(), ptxt.end(), 'u', 'l');
std::replace(ptxt.begin(), ptxt.end(), 'v', 'm');
std::replace(ptxt.begin(), ptxt.end(), 'w', 'n');
std::replace(ptxt.begin(), ptxt.end(), 'x', 'o');
std::replace(ptxt.begin(), ptxt.end(), 'y', 'p');
std::replace(ptxt.begin(), ptxt.end(), 'z', 'q');
This change addresses all the warnings in the build log, and also happens to fix all the errors as well.
If you're curious what the errors were: they were just a sort of verbose way of telling you that your arguments to std::replace() were not correct. Multi-characters happen to have the type int instead of char, and std::replace() does not accept taking an int for the last argument when the second to last argument is a char.
this has type `int`
|
V
std::replace( ptxt.begin(), ptxt.end(), 'l', '12');
^
|
this has type `char`... mismatch error between `int` and `char`
Taking issues one at a time is a good way to avoid being overwhelmed by all of them at once. Just take them one at a time, starting from the beginning. Often fixing one issue reported earlier will make other warnings and errors go away so you never have to bother with them.
Do not ignore warnings. Even though your programs may compile and work without all the warnings being fixed, it's a bad habit to ignore them. In fact, you should enable more warnings if you can, and pay attention to those too.