Caesar cipher - What's wrong? [closed] - c++

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'm having the problem with launching encrypting program. For single words strings it works well (for example, abcd), but when i type two or three or more words (a sentence, for example, abcd ab ac) it doesn't ask for a key, but rewrites the sentence I typed. What am I doing wrong? Thanks in advance. The code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int key, l;
char choose;
string message;
cout<<"Type the message"<<endl;
cin>>message;
cout<<"Give me a key from 0 to 26"<<endl;
cin>>key;
for (int i=0,l=message.size(); i<=l; i++)
{
if (isalpha(message[i]))
{
if (isupper(message[i]))
{
cout<<(char)('A'+(message[i]-'A'+key)%26);
}
if (islower(message[i]))
{
cout<<(char)('a'+(message[i]-'a'+key)%26);
}
}
else
{
cout<<message[i];
}
}
return 0;
}

Use
std::getline(std::cin, message);
cin does only read until the next whitespace, the rest of the input is being kept in the stream buffer so that you get the next word of your sentence entered in the first step as input for your second cin.

Related

C++ GetKeyState with a string [closed]

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.

Reading from a .txt file to a vector of doubles in C++ [closed]

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 3 years ago.
Improve this question
I am trying to read values from a .txt file to a vector (which is a member of a class) in C++, but despite the .txt having around 1000 lines, the vector is of size 0. I inserted a 'cout', and I know the file is opened and closed. I'm not sure what I could be doing wrong for the code not to read the contents of the .txt.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "option_class.h"
using namespace std;
int main(){
double cprice = 0.0;
int i = 0;
string line;
ifstream is;
is.open("/Users/<USER>/Desktop/SPY.txt");
if (!is){
cout << "Unable to open file" << endl;
return(0);
}
while(!getline(is, line).eof()){
is >> cprice;
option1.price.push_back(cprice);
}
is.close();
cout << "Closing file" << endl;
}
Have you tried something simpler:
while (is >> cprice)
{
option1.price.push_back(cprice);
}
The operator>> will skip whitespace, which includes newlines. There is no need to read a line at a time.

How to redirect text to a file in C++ [closed]

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 7 years ago.
Improve this question
i am trying to write a small automated program to calculate some values for me and output some text to a simple .txt file. do the redirection symbols < > & << >> work the same in C++ as they do in the command line for batch scripts? When i try to search how to redirect to a .txt file in C++. All of the examples, and tutorials i have found are presented in a manner that assumes IO on the console like the following.
cout::<<"show this text on the console";
cin::>> whatever you would call here to accept user input.
what i want to know is will it work to do it this way?
#include <string>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
if {
(X=0)
zero>>C:\test.txt;
x+5;
} return 0;
}
Your code does not work. I am not absolutely sure what is the desired behaviour, but this code writes the string zero to a file:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
ofstream myFile("C:\\Data\\test.txt");
//a condition which is always true
if (X==0)
{
myFile<<zero;
X + 5; //this is valid but useless
}
return 0;
}
#include <fstream>
int main(){
string zero = "touchPress 0 483 652\n";
std::ofstream fout("test.txt"); // creates new test.txt in folder where .exe is
fout << zero; //same as cout << zero;//but in the file
return 0;
}
fout as cout, i just reworked your barely alive program. is this what you wanted?

large text file processing error [closed]

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
everyone.
The program as shown below isn't executed for large text file, for example, 30GB.
The program is for simply converting text file format.
Let me know how to solve the issue.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include </usr/include/pcl-1.7/pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int main (int argc, char** argv)
{
int r,g,b;
if(argc!=3)
{
fprintf(stderr,"Usage:%s\n(1)Input_XYZRGB_filename\n(2)Output_PCD_filename\n",argv[0]);
exit(1);
}
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
std::ifstream ifs(argv[1]);
std::string buf;
for(size_t i=0; ifs && getline(ifs, buf); i++)
{
// std::cout << buf << std::endl;
std::istringstream is(buf);
pcl::PointXYZRGB pnt;
is >> pnt.x
>> pnt.y
>> pnt.z
>> r
>> g
>> b;
pnt.r= (uint8_t)r;
pnt.g= (uint8_t)g;
pnt.b= (uint8_t)b;
cloud->push_back ( pnt );
}
pcl::io::savePCDFileASCII(argv[2], *cloud);
return 0;
}
Make sure that your file input/output library supports large files. You can read the documentation. Also check the size of the file position parameter in the seek operation. In order to support large files it has to be 64 bit, not 32. In MS Visual C++, standard iostream seems does not support large files. But you can use other, low level input/output functions: _sopen_s, _read, _close, _lseeki64, etc. In gcc and mingw, you can use functions: _sopen, read, close, lseek.

Input file lines to array errors [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to read a file with full path and get each line and put them into an array. my code is like this:
#include <fstream>
#include <iostream>
using namespace std;
void main(){
int Log[200];
int i;
For(int i=0; i<30; i++)
{
getline(/var/asl/data/audit/20130502/20130502-0611/20130502-61157-UYHEZX8AAAEAAAbKRvKAAAAC, line);
Log[i] = line;
cout << Log[i] < "\n";
}
}
but the below errors come to me and I do not how to solve them. Can anyone help me?
log1.cpp:7: error: :main must return int
log1.cpp: In function int main():
log1.cpp:12: error: expected primary-expression before int
log1.cpp:12: error: expected before token
Another question I have is that if I want to search a special character that is in the line that stored in arrays,(I mean search in an array) what can I do?
Thanks a lot dear users for your reply. I tried the code and it does not have any errors. But when i run it nothing happen. My file is not in text format. it is as like as Apache server logs format. Should it be in text format? The other question is if i put these line in arrays can i search a special value in it?
Thanks for your reply in advance.
Salam ,Try this:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string line;
ifstream myfile ("example.txt"); //file address
string Log[200];
int i=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
Log[i] = line;
i++;
cout << line << endl;
}
myfile.close();
}
return 0;
}