#include <iostream>
using namespace std;
int main()
{
uint64_t a = 0xffffffffffffffff;
int c = 3;
char b[16];
sprintf(b, "%X", a);
cout << b << endl;
return 0;
}
This is printing and storing half of the fs, i need the rest of them, how do i do this?
You are invoking undefined behavior by passing data having wrong type.
The type format to print uint64_t in uppercase hex is PRIX64, defined in the header <cinttypes>.
Also don't forget to allocate for terminating null-character.
#include <iostream>
#include <cstdio> // sprintf()
#include <cinttypes> // PRIX64 (and its family) is defined in this header
using namespace std;
int main()
{
uint64_t a = 0xffffffffffffffff;
int c = 3;
char b[17]; // allocate also for terminating null-character
sprintf(b, "%" PRIX64, a); // use correct format
cout << b << endl;
return 0;
}
Related
i have write a code for addition with the variable long long but the summary is not like the normal addition
here is the code :
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
long long int a, b, c;
cout << "" << "";
cin >> a >> b;
c = abs(a) + abs(b);
cout << c;
cout << "\n";
}
when i input number like 1000000000000000000 2
the outpout is 1486618626 not 1000000000000000002
The old C function ::abs from <stdlib.h> takes and returns int, which cannot hold values that big on your platform.
Use std::abs from <cmath> (C++17 and later) or <cstdlib> instead.
Also, get rid of that using namespace std; and properly qualify the names instead. See Why is "using namespace std" considered bad practice?
Complete code:
#include <iostream>
#include <cstdlib>
int main() {
long long int a, b;
std::cin >> a >> b;
long long int c = std::abs(a) + std::abs(b);
std::cout << c;
std::cout << "\n";
}
Try using <cmath> instead of <stdlib.h>.
Also, don't add values while abs()ing them. Do it this way.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long int a, b;
long long int c;
cin >> a >> b;
a = abs(a);
b = abs(b);
c = a + b;
cout << c;
cout << endl;
}
Code works just fine.
Input and output:
1000000000000000000 2
1000000000000000002
I'm stuck with error message deprecated conversion from string constant to 'char*'
What I tried to do here is to assign "First", "Last" to cfoo1 and make cfoo2 equal to cfoo1. Lastly, display cfoo1 and cfoo2 to standard output.
#include <iostream>
#include <cstring>
#include "cfoo.h"
using namespace std;
CFoo :: CFoo(char first[], char last[]){
m_first[BUF] = first[BUF];
m_last[BUF] = last[BUF];
}
void CFoo :: WriteFoo(){
cout << m_first[BUF] << ", " << m_last[BUF];
}
#ifndef CFOO_HEADER
#define CFOO_HEADER
#include <iostream>
#include <cstring>
using namespace std;
const int BUF = 256;
class CFoo{
public:
CFoo(char first[], char last[]);
void WriteFoo();
private:
char m_first[BUF];
char m_last[BUF];
};
#endif
#include <iostream>
#include "cfoo.h"
using namespace std;
int main(){
CFoo foo1("Jong", "Yoon");
CFoo foo2 = foo1;
cout << "foo1 = ";
foo1.WriteFoo();
cout << endl;
cout << "foo 2 = ";
foo2.WriteFoo();
cout << endl;
return 0;
}
There are two issues:
Using string literals (which are of type char const*) to call a function that expects char[].
Trying to assign to char arrays.
Fixes:
Change the constructor to:
CFoo(char const* first, char const* last);
Change its implementation to:
CFoo(char const* first, char const* last)
{
// Make sure to copy at most BUF-1 characters
// to m_first and m_last.
m_first[0] = '\0'
strncat(m_first, first, BUF-1);
m_last[0] = '\0'
strncat(m_last, last, BUF-1);
}
You also need to change the implementation of CFoo::WriteFoo() to use the entire string
void CFoo::WriteFoo()
{
cout << m_first << ", " << m_last;
}
Also,
Accessing m_first[BUF] or m_last[BUF] is an error since the maximum value of a valid index to access those arrays is BUF-1.
When I input two integers, the output is correctly their difference. However when I enter a string and a char, instead of returning how many times the char appears in the string, it returns -1, which is the out put for error. Could anyone please help me? It's just my second day learing c++...
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void mycount(int a, int b)
{
std::cout<< a - b <<std::endl;
}
void mycount(char str[], char s[])
{
int len,i;
int sum=0;
len = strlen(str);
for (i=0;i<len;i++){
if (strncmp(&str[i],&s[0],1) == 0){
sum = sum + 1;
};
};
printf("results: %d times\n",sum);
}
int main()
{
int a,b;
char c[200],d;
if(std::cin>> a >> b){
mycount(a,b);
}
if(std::cin>> c[200] >> d){
mycount(a,b);
}
else{
std::cout<< "-1" <<std::endl;
}
std::cin.clear();
std::cin.sync();
}
Hint - what will this program print?
#include <iostream>
using namespace std;
int main()
{
char c[200],d;
cout << sizeof(c) << endl;
cout << sizeof(d) << endl;
return 0;
}
Answer:
200
1
That declaration does not do what you think it does - c is an array of 200 chars, d is a single char. It's a feature of the C declaration syntax, same as:
int *c, d;
c is a pointer to int, d is an int.
Since you are doing C++, why not make your life easier and use std::string instead?
A few changes should fix your problems. First when inputting an array with cin use getline and call ignore right before hand. I find it easier to pass s as a char instead of an array of size one make sure your call your second my count with c and d instead of a and b.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void mycount(int a, int b)
{
std::cout<< a - b <<std::endl;
}
void mycount(char str[], char s)
{
int len,i;
int sum=0;
len = strlen(str);
for (i=0;i<len;i++){
if (strncmp(&str[i],&s,1) == 0){
sum = sum + 1;
};
};
printf("results: %d times\n",sum);
}
int main()
{
int a,b;
char c[200],d;
if(std::cin>> a >> b){
mycount(a,b);
}
std::cin.ignore();
if(std::cin.getline (c,200) && std::cin >> d){
mycount(c,d);
}
else{
std::cout<< "-1" <<std::endl;
}
std::cin.clear();
std::cin.sync();
}
These changes should fix it.
I am trying to convert string value into its hex form, but not able to do.
Following is the C++ code snippet, with which I am trying to do.
#include <stdio.h>
#include <sys/types.h>
#include <string>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
using namespace std;
int main()
{
string hexstr;
hexstr = "000005F5E101";
uint64_t Value;
sscanf(hexstr.c_str(), "%" PRIu64 "", &Value);
printf("value = %" PRIu64 " \n", Value);
return 0;
}
The Output is only 5, which is not correct.
Any help would be highly appreciated.
Thanks,
Yuvi
If you're writing C++, why would you even consider using sscanf and printf? Avoid the pain and just use a stringstream:
int main() {
std::istringstream buffer("000005F5E101");
unsigned long long value;
buffer >> std::hex >> value;
std::cout << std::hex << value;
return 0;
}
#include <sstream>
#include <string>
using namespace std;
int main(){
string myString = "45";
istringstream buffer(myString);
uint64_t value;
buffer >> std::hex >> value;
return 0;
}
I would like to know what is the easiest way to convert an int to C++ style string and from C++ style string to int.
edit
Thank you very much. When converting form string to int what happens if I pass a char string ? (ex: "abce").
Thanks & Regards,
Mousey
Probably the easiest is to use operator<< and operator>> with a stringstream (you can initialize a stringstream from a string, and use the stream's .str() member to retrieve a string after writing to it.
Boost has a lexical_cast that makes this particularly easy (though hardly a paragon of efficiency). Normal use would be something like int x = lexical_cast<int>(your_string);
You can change "%x" specifier to "%d" or any other format supported by sprintf. Ensure to appropriately adjust the buffer size 'buf'
int main(){
char buf[sizeof(int)*2 + 1];
int x = 0x12345678;
sprintf(buf, "%x", x);
string str(buf);
int y = atoi(str.c_str());
}
EDIT 2:
int main(){
char buf[sizeof(int)*2 + 1];
int x = 42;
sprintf(buf, "%x", x);
string str(buf);
//int y = atoi(str.c_str());
int y = static_cast<int>(strtol(str.c_str(), NULL, 16));
}
This is to convert string to number.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
int convert_string_to_number(const std::string& st)
{
std::istringstream stringinfo(st);
int num = 0;
stringinfo >> num;
return num;
}
int main()
{
int number = 0;
std::string number_as_string("425");
number = convert_string_to_number(number_as_string);
std::cout << "The number is " << number << std::endl;
std::cout << "Number of digits are " << number_as_string.length() << std::endl;
}
Like wise, the following is to convert number to string.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
std::string convert_number_to_string(const int& number_to_convert)
{
std::ostringstream os;
os << number_to_convert;
return (os.str());
}
int main()
{
int number = 425;
std::string stringafterconversion;
stringafterconversion = convert_number_to_string(number);
std::cout << "After conversion " << stringafterconversion << std::endl;
std::cout << "Number of digits are " << stringafterconversion.length() << std::endl;
}
Use atoi to convert a string to an int. Use a stringstream to convert the other way.