Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i want to convert a sentence like 'good boy' to ascii code . i know the code that is a loop and print the ascii code of each character of sentence but i don't want this. i want that the ascii code of sentence (all characters alltogether) in long for example 1259788712..
You can use string to handle it.
#include <iostream>
#include <sstream> // use stringstream
using namespace std;
// turn int into string
string IntTOstring(int);
int main(void)
{
string sIn,sOut;
// input
sIn = "good boy";
sOut="";
for (int i=0 ; i<sIn.length() ; i++ ) {
// get one char from sIn each time
int temp=sIn.c_str()[i];
// turn int into string & save in sOut
sOut += IntTOstring(temp);
}
cout << sOut << endl;
return 0;
}
// use stringstream to convert int to string
string IntTOstring(int i){
stringstream ss;
ss << i;
return ss.str();
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need your help for C ++, I need to use a GetKeyState with a string
Example (does not work) :
string test = "0x47" // G Key for examble(https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes)
GetKeyState(test)
{
cout << "Nice << endl;
}
I would just like to know how to use the GetKeyState function with a string instead of the key (GetKeyState (string) instead of: GetKeyState (0x47))
GetKeyState() takes an int as input, not a string. So you MUST parse the string to convert it into an int, there is no other option.
There are many ways to do that parsing in C++, eg:
std::stoi():
#include <string>
std::string test = "0x47";
int value = std::stoi(test, nullptr, 0/*or 16*/);
GetKeyState(value)
std::sscanf():
#include <string>
#include <cstdio>
std::string test = "0x47";
int value = 0;
std::sscanf(test.c_str(), "%x", &value);
GetKeyState(value)
std::istringstream:
#include <string>
#include <sstream>
#include <iomanip>
std::string test = "0x47";
int value = 0;
std::istringstream(test) >> std::hex >> value;
GetKeyState(value)
Just to name a few.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a problem because this is my first time encountering some functions from C that I need to convert into C++.
Code:
FILE *pokf;
char name[10],gender;
int age, i, n, b1=0, b2=0;
float p;
pokf=fopen("Data.txt","r");
while (feof(pokf)==0) {
fscanf (pokf,"%s %c %d %f", &name, &gender, &age, &p);
if (gender=='Z') b1++;
if (p>=4.50 ) b2++; }
fclose(pokf);
I can write:
ifstream input;
input.open("Data.txt");
But then I don't know what to use instead of pokf because I can't use FILE *pokf anymore.
What to use instead of functions feof and fscanf?
The rough equivalent of fscanf is operator>> and I wouldn't worry about feof since it's being used incorrectly. So
while (feof(pokf)==0) {
fscanf (pokf,"%s %c %d %f", &name, &gender, &age, &p);
...
becomes
while (pokf >> name >> gender >> age >> p) {
...
Although using char name[10] in C++ will work, it has the obvious problem that you are limited to names of 9 characters of less. In C++ you should use std::string instead of a char array.
You should be able to use FILE *pokf in c++, here is an example http://www.cplusplus.com/reference/cstdio/feof/ where you can see how to open files in c++
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have been asked to develop program to count no. of lines and words in the file, this is my trial, my teacher said that I cant use >> operator for counting words and comparing but I could not handle it.
#include <iostream>
#include <fstream>
using namespace std;
int numberLines = 0;
int numberWords = 0;
void numberOfLines(){
cout<<"number of lines is " << numberLines << endl;
}
void numberWords(){
cout << "number of words is " << numberWords <<endl;
}
int main(){
string line;
char a = '';
ifstream myfile("files.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
numberLines++;
}
if ( a == ' '){
NumberWords++;
}
}
myfile.close();
}
numberOfLines();
numberOfWords ();
}
What you can do is add a 3rd argument to getline(). This lets it pull data from the stream until it hits a character. Doing getline(cin, line, ' ')takes all the data until the next and puts it into line. Your code might look like:
while(getline(inFile, line))
{
++numlines;
stringstream lineStream(line);
while(getline(lineStream, line, ' '))
{
++numWords;
}
}
The outer loop goes through the file and stores each line into line, then the inner goes through that line and counts each space. which correlates to a word.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to read from file line by line and print it on the screen:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream out("note.txt");
for (int i = 0; i < 10; i++)
out << i << " " << (i<<1) << "\n";
out.close();
ifstream fin;
fin.open("note.txt");
string line;
for (int i = 0; i < 10; ++i)
{
getline(fin, line);
cout << line << "\n";
}
return 0;
}
Is this approach correct? Cant I do it without a string variable (without string line in code)?
Instead of using a for loop you can use a while loop:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
string line;
ifstream out("note.txt");
while(getline(out, line)) {
cout << line << endl;
}
out.close();
}
If you are forced not to use strings then you can try a char buffer char buf[1024]. It must be pointed out that this approach is dangerous and error prone. If a line has more than 1024 characters then a buffer overflow will occur. Buffer overflow is the cause of many vulnerabilities and crashes. That being said, if you really have to use this method I would suggest you to be very careful by making the appropriate checks.
Copying a file verbatim is a simple as streaming out its stream buffer:
ifstream fin;
fin.open("note.txt");
std::cout << fin.rdbuf();
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
#include<fstream>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
ifstream infile;
ofstream outfile;
infile.open("oldfile.txt");
outfile.open("newfile.txt");
while(infile){
string str,nstr;
infile>>str;
char charr[10];
charr[0]='<';charr[1]='\0';
nstr=str.substr(0,str.find_first_of(charr));
outfile<<nstr<<' ';
}
}
this program uses substr(0, string.find_first-of(charcter array which is starting point to be substring))each word's unnecessary sub strings but it doesn't preserve the line number when writing to another file. can you fix it . it writes the file word by word sequencially. the code didn't preserve line by line,
string input doesn't care about line boundaries, it treats \n,\t,\v and probably others the same as space.
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line,word;
char foo[] = "<";
while ( getline(cin,line) ) {
string newline;
for ( istringstream words(line)
; words >> word ; ) {
newline+=word.substr(0,word.find_first_of(foo))+' ';
}
cout << newline << '\n';
}
}
Change
outfile<<nstr<<' ';
to
outfile<<nstr<<endl;
This will write line by line instead of sperating with a single whitespace character.