How to re sharp (join) arabic letters in c++ - visual-c++

well i need help in solving this issue, i want to join the arabic letters in a way to look like this " السلام عليكم " not like this " ا ل س ل ا م ع ل ي ك م "
here's my code:
#include<iostream>
#include<fstream>
#include<string>
#include<Windows.h>
#include<io.h>
#include<fcntl.h>
#include<stdlib.h>
using namespace std;
int main(){SetConsoleOutputCP(1256);
SetConsoleCP(1256);
string name;
string line;
unsigned int curline=0;
cin >> name;
ifstream thefile;
thefile.open("text.txt");
while (getline(thefile, line)){
curline++;
if (line.find(name, 0) != string::npos && name.size() ==
line.size())
cout << "found" << name<<curline;
}
thefile.close();
system("pause");
return 0;
}
now in this code i'm getting the words from a file.

Related

How to fix C++ argument of type"const char*" is incompatible with parameter of type"char*"

I'm using vs2019 and I have a problem the compiler give me an error however I cant solving that
please help me.
the error is : argument of type"const char*" is incompatible with parameter of type"char*"
Blockquote
*** #include <iostream>
#include<cstring>
using namespace std;
class part
{
public:
char partname[30];
int partnumber;
double cost;
void setpart(char pname[], int pn, double c)
{
strcpy_s(partname, pname);
partnumber = pn;
cost = c;
}
void showpart()const
{
cout << "\npartname : " << partname;
cout << "\npartnumber : " << partnumber;
cout << "\ncost($) : " << cost;
}
};
int main()
{
part part1, part2;
part1.setpart("handle bolt", 467, 4500);
part2.setpart("start lever", 549, 2300);
cout << "\nFirst part : "; part1.showpart();
cout << "\nSecond part : "; part2.showpart();
}***
The strings you are giving to setpart are const strings ( const char *) . But set part take a char * as parameter. Since pname will not be modified in your example you can replace void setpart(char pname[], int pn, double c) by void setpart(const char pname[], int pn, double c)

String concatenation with user input in C++

I tried to write a code which gets an input from the user and concatenate with another string but it doesn't work well. The code is down below,
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
cin>>s2;
s1=s1+s2
cout<<s1;
return 0;
}
Input:
this is how it works
Expected Output:
Hi this is how it works
But it didn't work as I expected. The output was:
Hi this
Can anybody help me?
'>>' reads space-delimited strings.
Now I found getline is used to read lines.
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
getline(cin,s2);
s1=s1+s2;
cout<<s1;
return 0;
}
Now I get the desired output.
#include <iostream>
using namespace std;
int main()
{
string s1="hi ";
string s2;
cout << "Enter string s2: ";
getline (cin,s2);
s1 = s1 + s2;
cout << "concating both "<< s1;
return 0;
}
here use this! this should help!

cin unintentionally skipping user input

I am trying to write a loop that validates user input, and then repeats if the input is bad. The input must be either a binary number (as a string) or a decimal number (as an int). I have seperate functions to validate this input, but they are not causing any trouble.
The problem arises when I select 1 or 2, and then willingly enter an invalid binary or decimal number. At this point, the do-while loop repeats successfully. The program prints another request for user input to cout, But when it comes time for the user to enter input, the program thinks that there is input in the console before I even enter anything. I believe this is a problem with whitespace/control characters in the buffer, but I am not sure how to fix it. I have tried using std::cin >> std::ws to clear any straggling white space, but no luck.
#include <iostream>
#include <string>
#include <limits>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using std::numeric_limits;
using std::max;
using std::streamsize;
using std::string;
//int toDecimal;
//true is is binary
bool validateBinary(const string &binaryNumber){
for(int i = 0; i < binaryNumber.length(); i++){
if((binaryNumber[i] != 1) && (binaryNumber[i] != 0)){
return false;
}
}
return true;
}
//true if is decimal
bool validateDecimal(){
return cin;
}
int main() {
int conversionType = 0; //we initialize conversionType to a default value of 0 to ensure the copiler it will always have a value
bool isBinary = false;
bool isDecimal = false;
string binaryNumberInput;
int decimalNumberInput;
do {
if(conversionType == 0){
cout << "Enter 1 to convert binary to decimal," << endl;
cout << "2 to convert decimal to binary, " << endl;
cout << "or 3 to exit the program: ";
std::cin >> std::ws; //to clear any whitespace fron cin
cin >> conversionType; //upon a second iteration, this value is read in before a user input is given
}
if(!cin || (conversionType != 1 && conversionType != 2)){
cout << "Incorrect input." << endl;
cin.clear(); //clear the fail bit
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //used to ignore not-numeric input
}
cout << "You have selected option " << conversionType << "." << endl;
if(conversionType == 1){
cout << "Please enter a binary number: ";
cin >> binaryNumberInput;
isBinary = validateBinary(binaryNumberInput);
if(!isBinary){
cout << "The numbered you entered is not a binary number!" << endl;
conversionType = 0;
}
}
if(conversionType == 2){
cout << "Please enter a decimal number: ";
cin >> decimalNumberInput;
isDecimal = validateDecimal(); //true if succeeded, meaning is a number
if(!isDecimal){
cout << "The numbered you entered is not a decimal number!" << endl;
conversionType = 0;
}
}
}
while((conversionType != 1 && conversionType != 2) || (isBinary == isDecimal));
return 0;
}
Rather than debug your current program you might want to consider using the standard library to simply things
#include <iostream>
#include <string>
#include <bitset>
#include <climits>
#include <limits>
template<typename T>
void get(T& value)
{
while (!(std::cin >> value)) {
std::cout << "Invalid input\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
int main()
{
std::cout << "Enter 1 to convert binary to decimal,\n" <<
"2 to convert decimal to binary\n";
int option;
if (std::cin >> option) {
switch (option) {
case 1: {
std::bitset<CHAR_BIT * sizeof(unsigned long long)> bits;
get(bits);
std::cout << bits.to_ullong() << '\n';
break;
}
case 2: {
unsigned long long i;
get(i);
std::cout << std::bitset<CHAR_BIT * sizeof i>(i) << '\n';
break;
}
}
}
}
If you want this to loop you should be able to add it back in again easily enough.

Outputting large structures to HD

I have an array of a large structure that I am trying to output to hard drive. I seem to be able to write to hard drive just fine (though it's difficult to verify by looking at the binary data), however when I try to read it back, I always end up with a garbled mess. Any ideas what I'm doing wrong?
here's the structure configuration:
class xyz
{
public:
double x, y, z;
};
class trianglePackage
{
public:
int score;
int position;
xyz contactCoordinates;
xyz normalVector;
xyz locatorOffset;
};
class quadanglesOutput
{
public:
int locator1position, locator2position, locator3position, locator4position;
xyz centroid;
int surfaceAreaScore;
int centroidDifferance1Score;
int centroidDifferance2Score;
int minDistance1Score;
int minDistance2Score;
int totalLocatorScore;
int totalHullScore;
int totalScore;
double surfaceArea;
double centroidDifferance1;
double centroidDifferance2;
double minDistance1;
double minDistance2;
int hull;
trianglePackage locator1, locator2, locator3, locator4;
};
and here are the read/write functions I'm using:
void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{
string outputName = parameters.fileName + " " + description + ".bin";
cout << "Output " << outputName.c_str() << "...";
ofstream output2;
output2.open(outputName.c_str());
output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
output2.close();
cout << "done" << endl;
}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
string fileName = parameters.fileName + " " + description + ".bin";
cout << "openining " << fileName << "...";
ifstream readFile;
readFile.open(fileName.c_str());
readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
readFile.close();
cout << "done" << endl;
}
Typically the arrays of structures are about 100 in length, but usually only about the first 25 read back correctly, everything else is default uninitialized data.
I'm 99% sure that it's something wrong with my code, however is there a possibility it has something to do with four byte alignment?
Thanks.
It may be an issue with byte alignment, use pragma.
try wrap classes around with
#PRAGMA PACK PUSH(1)
....
#PRAGMA PACK POP
or
#PRAGMA PACK(1)
struct{
..
}
Try those as well:
Force binary flag for the stream.
ios_base::binary
readFile.open(fileName.c_str(), ios_base::binary);
Try to flush the the stream.
stream.write(...)
stream.flush()
//i know that close() should flush it.
UPDATE:
Everything works for me:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
#pragma pack(1)
class xyz
{
public:
double x, y, z;
};
#pragma pack(1)
class trianglePackage
{
public:
int score;
int position;
xyz contactCoordinates;
xyz normalVector;
xyz locatorOffset;
};
#pragma pack(1)
class quadanglesOutput
{
public:
int locator1position, locator2position, locator3position, locator4position;
xyz centroid;
int surfaceAreaScore;
int centroidDifferance1Score;
int centroidDifferance2Score;
int minDistance1Score;
int minDistance2Score;
int totalLocatorScore;
int totalHullScore;
int totalScore;
double surfaceArea;
double centroidDifferance1;
double centroidDifferance2;
double minDistance1;
double minDistance2;
int hull;
trianglePackage locator1, locator2, locator3, locator4;
};
class param
{
public:
string fileName;
int topXlist;
};
void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{
string outputName = parameters.fileName + " " + description + ".bin";
cout << "Output " << outputName.c_str() << "...";
ofstream output2;
output2.open(outputName.c_str());
output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
output2.close();
cout << "done" << endl;
}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
string fileName = parameters.fileName + " " + description + ".bin";
cout << "openining " << fileName << "...";
ifstream readFile;
readFile.open(fileName.c_str());
readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
readFile.close();
cout << "done" << endl;
}
int main(int argc, char *argv[])
{
quadanglesOutput a = {0};
cout<<"total score:"<<a.totalScore<<endl;
cout<<"locator position:"<<a.totalScore<<endl;
cout<<"locator position:"<<a.locator1.position<<endl;
cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
cout<<"sizeof quadangsomething:"<<sizeof(quadanglesOutput)<<endl;
a.totalScore=1;
a.locator1.position=333445;
a.locator2.normalVector.y = 999.3224;
cout<<"total score:"<<a.totalScore<<endl;
cout<<"locator position:"<<a.locator1.position<<endl;
cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
param p = {"C:/", 1};
outputQuadangleOutput(&a, "file1", p);
quadanglesOutput *b = new quadanglesOutput();
readIn(b, p, "file1");
cout<<"new total score:"<<b->totalScore<<endl;
cout<<"new locator position:"<<b->locator1.position<<endl;
cout<<"new locator position:"<<b->locator2.normalVector.y <<endl;
delete b;
string asdf;
cin>>asdf;
};
OUTPUT:
total score:0
locator position:0
locator2.normalVector.y :0
sizeof quadangsomething:436
total score:1
locator position:333445
locator2.normalVector.y :999.322
Output C:/ file1.bin...done
openining C:/ file1.bin...done
new total score:1
new locator position:333445
new locator2.normalVector.y :999.322
without pragma it's still correct but you can see the difference in size:
sizeof quadangsomething:440
But packing it is good when sending structures over network.
Because here system alligns it always in the same fashion.

Why doesn't unsigned char* work with ifstream::read?

I am a beginner with C++. I have a new project at work where I have to learn it, so I'm trying some things just to test my understanding. For this problem, I'm trying to read a file and then print it on screen. Super simple, just trying to get good at it and understand the functions that I'm using. I copied some text from a MS Word document into a notepad (*.txt) file, and I'm trying to read this *.txt file. All of the text in the word document is bolded, but other than that there are no 'unusual' characters. Everything prints out on the screen as it appears in the document except the bolded " - " symbol. This character is printed as the "u" with a hat character ("so called extended ASCII" code 150). I try to print out the integer value of this character in my array (which should be 150) but I get -106. I realize this signed integer has the same bits as the unsigned integer 150. My question is how to get the output to say 150? Here's my code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
unsigned char* input1;
int input1size = 57;
ifstream file("hello_world2.txt",ios::binary | ios::ate);
if (file.is_open()){
int size;
size = (int) file.tellg();
cout <<"This file is " << size << " bytes." << endl;
file.seekg(0,ios::beg);
input1 = new unsigned char[input1size];
file.read(input1, input1size);
cout << "The first " << input1size <<" characters of this file are:" << endl<<endl;
for (int i=0; i<input1size; i++) {
cout << input1[i];
}
cout<<endl;
}
else {
cout <<"Unable to open file" << endl;
int paus;
cin>>paus;
return 0;
}
file.close();
int charcheck = 25;
int a=0;
int a1=0;
int a2=0;
unsigned int a3=0;
unsigned short int a4=0;
short int a5=0;
a = input1[charcheck];
a1 = input1[charcheck-1];
a2 = input1[charcheck+1];
a3 = input1[charcheck];
a4 = input1[charcheck];
a5 = input1[charcheck];
cout <<endl<<"ASCII code for char in input1[" << charcheck-1 <<"] is: " << a1 << endl;
cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] is: " << a << endl;
cout <<endl<<"ASCII code for char in input1[" << charcheck+1 <<"] is: " << a2 << endl;
cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned int: " << a3 << endl;
cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned short int: " << a4 << endl;
cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as short int: " << a5 << endl;
int paus;
cin>>paus;
return 0;
}
Output for all this looks like:
This file is 80 bytes.
The first 57 characters of this file are:
STATUS REPORT
PERIOD 01 u 31 JUL 09
TASK 310: APPLIC
ASCII code for char in input1[24] is: 32
ASCII code for char in input1[25] is: -106
ASCII code for char in input1[26] is: 32
ASCII code for char in input1[25] as unsigned int: 4294967190
ASCII code for char in input1[25] as unsigned short int: 65430
ASCII code for char in input1[25] as short int: -106
So it appears "int a" is always read as signed. When I try to make "a" unsigned, it turns all the bits left of the eight bits for the char to 1's. Why is this? Sorry for the length of the question, just trying to be detailed. Thanks!
What you're dealing with is the sign-extension that takes place when the char is promoted to int when you assign it to one of your a? variables.
All the higher order bits must be set to 1 to keep it the same negative value as was in the smaller storage of the char.

Resources