I have an input.txt and an output.txt, obviously I generate the output.txt, which for now is to write the exact same thing out, but it does not do that because of a possible encoding(?) error.
Here are pictures of the input and output:
My code looks like this:
Menu.cpp:
void Menu::kiir(){
ofstream myfile("output.txt");
if (myfile.is_open()){
myfile << sakk.toString();
}
}
The toString() method in sakktabla.cpp:
string sakktabla::toString(){
std::string eredmeny="";
int x = this->x;
int y = this->y;
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
eredmeny += (int)getValue(i, j);
}
eredmeny += "\n";
}
return eredmeny;
}
The getValue() method:
int sakktabla::getValue(int x, int y){
return this->_vec[x][y];
}
I don't see where you're ingesting, but you seem to be:
reading character input such as the character '2';
converting that to integer input, i.e. the integer 2;
outputting that integer, here producing an ASCII character with value 2.
Possibly the confusion is that std::string::operator+= doesn't do any sort of conversion.
You possibly want:
eredmeny += std::to_string(getValue(i, j));
std::to_string() converts a bunch of numeric types to their human-readable string counterparts.
Related
I am on a project where the major task is to read in from a file a a character from c string at a time using .get(), and store it in a char buffer, and convert the value to double if its double value like "20.12". I've done implementation of converting to double, but I am not sure how to show point to 2 decimal points.
since we are restricted from using any function from iomanip, secprecision or showpoint is not considered.
here is my implementation of CstrToDbl
double stuCstrToDbl(const char *num)
{
double value=0.0;
double decimal=1.00;
int decimalCount=0;
double decimalDiv=1.00;
int i=0;
while(num[i]!='\0')
{
i++;
}
for(int j=i-1; j>=0; j--) {
decimalCount++;
if (num[j]=='-') {
value*=-1;
}
else if(num[j]=='.'){
for(int i=0; i < decimalCount-1; i++)
{
decimalDiv*=10.00;
}
}
else {
value += (num[j] - '0') * decimal;
decimal *= 10;
}
}
value=value/decimalDiv;
return value;
}
I want to read a file into an int array , but I need to do that one element at a time.
Here's what's in the file :
1000320
0110313
3333000
2033000
2203011
2000010
And here is the code :
std::ifstream fin("file.in");
for (int i=0; i<6; ++i) //rows
{
for (int j=0; j<7; ++j) //columns
{
fin>>_array[i][j];
}
}
If I would print _array[0][0] it will output everything that's on the first line. However I want to split what's in the file into rows and columns .
What's the most elegant way to do this for an INT array ( I don't want to use a char array although it would be much easier . Also , I know that if I put spaces between the numbers in my file the program would read them one at a time but I want them to be like this )
You can try using getchar()
You'll get char but just convert it to int immediately
char temp = getchar(fin);
_array[i][j] = temp - '0';
Unfortunately there's no getint()
in case you want to keep the same structure
#include<fstream>
int main() {
int n;
fstream fin("urfile.txt");
string line;
int _array[6][7];
for (int i = 0; i < 6; ++i) //rows
{
std::getline(read, fin);
for (int j = 0; j < 7; ++j) //columns
{
_array[i][j] = line[j]-'0';
cout << _array[i][j];
}
cout << '\n';
}
fin.close();
}
I would like to know whether the following code is "valid":
#include <iostream>
using namespace std;
int main(void) {
string s="Hello World!\n";
for (int i=0;i<s.size();++i) {
for (int j=0;j<s[i];++j) {
cout << "+";
}
cout << ".>\n";
}
}
I made this code but I don't know if I should add some ".c_str" or else, to make it better code.
Thanks to all the contributions, I can now say my code is valid in C++, as the [] operator is part of the string class.
Furthermore, it can be added that
for (char c : s) {
for (int j = 0; j != c; ++j) {
/*..*/
}
}
is short for
for (int i = 0; i < s.size(); ++i) {
for (int j = 0; j < s[i]; ++j) {
/*..*/
}
}
Thank you to all of you!
This code has a potential problem:
for (int j=0;j<s[i];++j) {
The value s[i] is a char, which might be a signed type. If the character value is negative then this loop causes undefined behaviour due to eventual overflow of j.
On an ASCII system this exact code contains no negative characters, but in EBCDIC the newline is 0x85 which would translate to a negative character value if the system had plain char as signed.
To be on the safe side, it should be for (int j = 0; j < (unsigned char)s[i]; ++j). Or in the range-based version, use for (unsigned char c: s).
I have posted previously and got some very helpful responses. I need to read in people's info (such as ID, age, etc.) from a text file into different arrays.
Records are like this
2012317109 Jamie Carles Smith Male 65 (different bits of info are separated by TAB, lines ending with newline)
However, regarding the ID numbers, I am told to use extraction operator (<<) to get the ID number as an integer and not as a string.
Then I must sort these records by alphabetical string order and then put these in an output file.
So far, I have the following. How should I proceed? I am not to use maps or vectors, I need to use arrays.
#include <iostream>
#include <fstream>
using namespace std;
void selection_sort( double x[], int length)
{
int i, j, k;
double t;
for (i=0; i<length-1; ++i) {
k = i; //find next smallest elm, x[k]
for (j=i+1; j< length; ++j)
if (x[k] > x[j]) k = j;
//swap x[i] with x[k]
t = x[i]; x[i] = x[k]; x[k] = t;
}
}
int main () {
ifstream fin;
ofstream fout;
fin.open("input.txt");
fout.open("output.txt");
if (fin.fail()) {
cout << "Fail to open inout.txt" << endl;
exit(1);
}
struct row{string ID, name, rest;};
int x;
fout << x << endl;
row *rows=new row[x];
for (int i=0; i<x; ++i) {
getline (fin, rows[i].ID, '\t'); // ID
getline (fin, rows[i].name, '\t'); // name
getline (fin, rows[i].rest );
}
selection_sort (row[], x);
//complier error this line: expected primary expression before [ token.
}
The easiest approach is probably to use std::sort (from the header <algorithm>), and define an
bool operator<(row const& lhs, row const& rhs)
{
// delegates to operator< for std::string
return lhs.name < rhs.name;
}
and then call the STL algorithm
// uses the above defined operator< for row objects
std::sort(rows, rows + x);
and then write the sorted output.
I wrote this code in c++
#include <iostream>
using namespace std;
char convert1 (char x[])
{
for ( int i =0; i<5;i++)
{
if ( i/2 ==0 )
x[i] =tolower(x[i]);
else
x[i] = toupper(x[i]);
return x[i];
}
}
int main()
{
char z[] = "REDCA";
cout<<convert1(z);
return 0;
}
it will work but it always print only the small case of the first letter R. but I want to print the complete word like this
rEdCa
so how can i modify it please
You want if(i%2 == 0) rather than if(i/2 == 0) (probably a typo).
Also, you are only returning x[i], which is a single char. The better idea would be using std::string instead of char *, but you can make this work with char * as well. (I won't provide full code, as we have established in your previous question, this is your assignment and you should do it, not us.)
The return value of convert1 is char. So, cout << convert1(x) sees a char, and thus you just get the first r.
You probably want something like this:
template<size_t S>
char const* convert1(char (&x)[S])
{
for(int i = 0; i < S; ++i)
{
if(i%2 ==0) {
x[i] = tolower(x[i]);
} else {
x[i] = toupper(x[i]);
}
}
return x;
}
Want better, use a std::string.