im doing an assignment for my lab which has to do with encrypting a string into a text file from another file, and it works good but my only problem is that when i encrypt or decrypt the encryption omits the letter Z and Y. I've tried changing a couple of things but nothing works, please if you know how to explain because i am lost.
this is my code so far:
#include iostream
==
#include fstream
==
#include cstring
==
#include string
==
using namespace std;
int main()
{
ifstream inputFile;
ofstream outputFile;
const int SIZE=150;
char file[SIZE];
char message[SIZE+1];
char letter;
char alphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
cout<<"Enter the message you want to encrypt: "<<endl; //getmessage
cin.getline(message,SIZE,'\n');
for(int i=0;i<strlen(message);i++){
if(islower(message[i])) //change to
message[i]=toupper(message[i]); //uppercase
}
outputFile.open("textoNoEncrypt.txt");
for(int i=0;i<strlen(message);i++){ //copy message to
outputFile.put(message[i]); //file
}
outputFile.close();
inputFile.open("textoNoEncrypt.txt");
if (inputFile.is_open()){
int i=0;
while (!(inputFile=='\0')){
inputFile>>file[i]; //reading and copying
i++; //data from file
}
}
inputFile.close();
outputFile.open("textoEncrypt.txt");
for(int i=0;i<strlen(file);i++){
int j=0;
int P=0;
int index=0;
bool found=false;
while(!found){
if(file[i]==alphabet[j]){
P=j;
index=(P+3)%26; //encrypting message
file[i]=alphabet[index];
found=true;
cout<<j<<" ";
}
j++;
}
outputFile.put(file[i]);
}
outputFile.close();
cout<<message<<endl; //show original message
cout<<file<<endl; //show encrypted message
return 0;
}
I think this is cause by the P+3 line
Related
Hello and thanks to all who considered reading. I recently started learning c++ in a class and I am just now learning file input and output. I have the following program,
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Cups{
string owner;
public:
string type;
float size;
Cups(string owner, string type, float size){
this->owner = owner;
this->type = type;
this->size = size;
}
string get_owner(){
return owner;
}
};
vector<Cups> extract(string filename){
ifstream inFile;
string line;
string owner, type;
float size;
vector<Cups> o;
inFile.open(filename);
if(!inFile.is_open()){
cout<<"Error, file not found..."<<endl;
exit(1);
}
while(!inFile.eof()){
getline(inFile, line);
stringstream ss(line);
getline(ss, owner, ',');
getline(ss, type);
if(type.compare("Short")==0){
size = 1.0;
}
else if(type.compare("Tall")==0){
size = 1.5;
}
else if(type.compare("Grande")==0){
size = 2.0;
}
else if(type.compare("Venti")==0){
size = 2.5;
}
else{
cout<<"Error, "<<owner<<"'s cup size not found."<<endl;
exit(1);
}
o.push_back(Cups(owner, type, size));
}
inFile.close();
return o;
}
int main(int argc, char** argv){
if(argc < 2){
cout<<"Please compile with and orders file."<<endl;
exit(1);
}
int i;
vector<Cups> orders = extract(argv[1]);
for(i = 0; i < orders.size(); i++){
cout<<orders[i].get_owner()<<", "<<orders[i].type<<endl;
cout<<orders[i].size<<endl;
}
}
with the following file, input as an executable argument
Natassa,Grande
Demy,Tall
Elena,Short
The program outputs the warning when you compile without a file just fine, however when running it, it outputs
Natassa, Grande
2
Demy, Tall
1.5
Elena, Short
1
, Short
1
I've tried running it in gdbonline and there seems to be a problem with my extraction perhaps, but I can't figure it out. Thanks for all who bothered to read and a bigger thanks to any who help.
Edit: Added the not symbol ot the inFile.eof() check, but still getting the ghost line shown above. I would post to codereview but this code is still not working correctly.
This c++ code giving me errors and I dont know how to remove those errors.
code:
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
string& RaiseItToUpperCase(string& w)
{
int len = w.length();
for (int index =0; index <len; index++)
w[index]= toupper(w[index]);
return w;
}
void LoadData()
{
string filename;
while(true)
{
cout<<"Enter Data file:";
cin>>filename;
cout<<"Filename entered"<<filename<<endl;
ifstream myfile(filename.c_str());
if(!myfile.good())
{
cout<<"Please Enter a Valid text File"<<endl;
continue;
}
static std::string const targetExtension ("txt");
if (!(filename.size() >= targetExtension.size()
&& std::equal (filename.end() - targetExtension.size(),
filename.end(),
targetExtension.begin() ) ))
{
cout<<"File is not txt"<<endl;
continue;
}
break;
}
string i = filename;
string o;
cout<<"Enter an output file name:"<< endl;
cin>>o;
ofstream output;
ifstream input(filename);
output.open(o.c_str());
int charc =0;
int numw =0;
int longl =0;
int shortl =10000;
while (input>>1)
{
numw++;
charc = charc + i.length() +1;
if (i.length() > longl)
{
longl = i.length();
}
if (i.length() < shortl)
{
i =RaiseItToUpperCase(i);
output << i;
if(input.get() ==32)
{
output<<" ";
}
else
{
output<<"\n";
}
}
charc = charc - 1;
output<<"\nWord Counter Summary\n"<<endl;
output<<"Total Number of Words:"<<numw<<endl;
output<<"Total Number of Characters:"<<charc<<endl;
output<<" Largest Word Size:"<<longl<<endl;
output<<" Smallest Word Size"<<shortl<<endl;
}
}
int main ()
{
LoadData();
return 0;
}
This is c++ file stream program and I am trying to run this code but it giving me errors and i am not able to figure out how remove this errors
so Can anyone tell me how to remove those errors and make this code run
Update
here is the Error:
Error 1 error C2679: binary '>>' : no operator found which takes a
right-hand operand of type 'int' (or there is no acceptable
conversion) c:\users\acer\documents\visual studio
2012\projects\consoleapplication15\consoleapplication15\source.cpp 52 1 ConsoleApplication15
And thanks in advance
What do you want to do in while (input>>1) ?
If you want read out something:
If you want to read to an integer like int8_t (or int16_t / int32_t etc.):
int8_t number = 0;
while (input >> number)
If you want to read to a char:
char ch;
while (input >> ch)
I am trying to read in two words from text file into one char array element. I CANNOT USE std::string. I get a seg fault because my loop is looping in a way that goes out of bounds. I cannot copy in the two words. please help me do this!! My loop for card struct works perfectly. I cannot get the "first last" into the people[].name
// deck of cards
// below are initializations
#include
#include
#include
#include
#include
using namespace std;
//globals
const int maxCards = 52;
//Structs
struct card {
char suit[8];
char rank[6];
int cvalue;
char location;
};
struct player {
char name[100];
int total;
card hand[4];
};
//program
int main()
{
//constants
char tempfName[100];
char templName[100];
//create struct array(s)
card deck[52];
card shuffledDeck[52];
player people[4];
//create pointers
card * deckPointer =NULL;
deckPointer = deck;
card * shuffledDeckPointer=NULL;
shuffledDeckPointer = shuffledDeck;
for(int i=0;i<4;i++)
{
strcopy(people[i].name,"first last");
}
//open player names file
ifstream fin2;
string fin2Name;
//get file name from user
cout << "Enter player file name...(Players.txt)" << endl;
getline(cin, fin2Name);
//open file
fin2.open(fin2Name.c_str());
//check if Players.txt opens correctly
if(!fin2.good())
{
cout << "Error with player file!" << endl;
return 0;
}
else
{
int j =0;
fin2 >> people[j].name; //prime file
while(fin2.good())
{
//find the length
int index =0, length=0;
while(tempfName[length] != '\0')
{
length++;
}
//now add space after first name
tempfName[length] = ' ';
length++;
while(templName[index] != '\0')
{
tempfName[length] = templName[index];
length++;
index++;
}
tempfName[length]='\0';
int counter =0;
while(templName[counter] != '\0')
{
people[0].name[counter] = templName[counter];
counter++;
}
}
}
it seems your tempfName is not pointing to a correct object in your while loop in else statement
else
{
int j =0;
fin2 >> people[j].name; //prime file
while(fin2.good())
{
//find the length
int index =0, length=0;
while(tempfName[length] != '\0')
{
length++;
}
You could do some cheating:
char full_name[256]; // Binary quantities are better.
snprintf(full_name, sizeof(full_name),
"%s %s",
first_name, last_name);
You should do more research regarding C-style string functions, like check a good reference book. It should have a chapter about the C-style string functions.
Here are some helpful ones:
strcpy, strcat, strchr, sprintf, strlen
Data input from the keyboard.The length of array maybe very long.
I want to finish inputing when press ENTER twice.
the numbers separated by space, tab or ",". how to detect the length n?
I have tried like this:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void main()
{
bool flag=true;
unsigned n=0,i;
double x;
string line,str;
istringstream iss;
cout<<"input your numbers."<<endl;
count<<"Press the Enter key twice finish data inputting."<<endl;
while(flag)
{
getline(cin,line);
str+=line+' ';
if(line.empty())
flag=false;
}
// get the length n
iss.str(str);
while(iss>>x)
{
n++;
}
double *v=new double[n];
iss.seekg(0);
for(i=0;i<n;i++)
iss>>v[i];
for(i=0;i<n;i++)
{
cout<<v[i];
if(i<n-1)
cout<<' ';
}
cout<<endl;
delete []v;
}
I am a novice. Help me, Please!
Try this After taking the input in variable line do this:
#include<iostream>
#include<string>
#include<sstring>
using namespace std;
void main()
{
bool flag=true;
unsigned n,i=0;
double x;
string line,str;
istringstream iss;
cout<<"input your "
getline(cin,line);
int c=0;
char * pch;
pch = strtok (line," ,");// this will work for space and comma but you can add your own specifiers
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,");
c++;
}
return(c);// will give the length of arry.
}
Man to declare 1D array
the syntax is
void main()
{
int array[5];
//to initalize;
for(int i=0;i<5;i++)
{
array[i]=i;
}
for(int i=0;i<5;i++)
{
cout<<array[i]<<" ";
}
// and to declare dynamically
int * array=new int[5];
}
I have solved this problem.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void main()
{
bool flag=true;
unsigned n=0,i;
double x;
string line,str;
istringstream iss;
cout<<"input your numbers."<<endl;
cout<<"Press the Enter key twice finish data inputting."<<endl;
//Brecause data may come from clipboard and have multi line.
while(flag)
{
getline(cin,line);
str+=line+'\n';
// vc++ 6.0 have a bug in include file: STRING. You shoud fix it.
//Or you shoud press ENTER more than twice to terminate inputing.
//Replace I.rdbuf()->snextc(); to _I.rdbuf()->sbumpc();
if(line.empty())
flag=false;
}
// get the length n
iss.str(str);
while(iss>>x)
{
n++;
}
double *v=new double[n];
iss.clear();// very important.
// initialize v[n]
iss.str(str);
for(i=0;i<n;i++)
iss>>v[i];
// output v
for(i=0;i<n;i++)
{
cout<<v[i];
if(i<n-1)
cout<<' ';
}
cout<<endl;
delete []v;
}
The data file is just an integer, a string, another integer.
For Example:
20 Bob 550
Here's the code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct RECORD
{
int ID;
string name;
float balance;
};
void Display(char fname[])
{
RECORD st;
fstream f;
f.open(fname, ios::in|ios::binary);
cout<<fixed<<showpoint<<setprecision(2);
for(int i=1; i<=5; ++i)
{
//read a record file from the RAF
f.read((char*)&st, sizeof(st));
cout<<st.ID<<'\t'<<st.name<<'\t'<<st.balance<<endl;
}
f.close();
}
int main()
{
//initalize the RAF with dummy record 0, "nnn", 0.0
fstream g;
g.open("data.raf", ios::out|ios::binary);
RECORD dummy={0, "nnn", 0.0};
for(int i=1; i<=5; ++i)
{
g.write((char*)&dummy, sizeof(RECORD));
}
g.close();
Display("data.raf");
//copy text file into RAF
fstream ftxt;
fstream fraf;
//open text file to read from
ftxt.open("data.txt", ios::in);
//open RAF to write
fraf.open("data.raf", ios::out|ios::binary);
for(int i=1; i<=5; ++i)
{
ftxt>>dummy.ID>>dummy.name>>dummy.balance;
int byteOfSet = (dummy.ID/10-1)*sizeof(RECORD);
//seekp to put (write)
//seekg to get (read)
fraf.seekp(byteOfSet, ios::beg);
//beg from beginning of file
fraf.write((char*)&dummy, sizeof(RECORD));
}
//deposit 100 in ID #40
int id;
cout<<"Enter an ID number: ";
cin>>id;
fraf.open("data.raf", ios::in|ios::out|ios::binary);
int byteOfSet = (id/10-1)*sizeof(RECORD);
fraf.seekg(byteOfSet, ios::beg);
fraf.read((char*)&dummy, sizeof(RECORD));
dummy.balance += 100;
//put the updated record back into the same place
fraf.seekp(byteOfSet, ios::beg);
fraf.write((char*)&dummy, sizeof(RECORD));
//fraf.close();
ftxt.close();
fraf.close();
Display("data.raf");
system("pause");
return 0;
}
The code runs then just dies in the console... I have no idea what's wrong. I think it might have something to do with reacessing the RAF but that's just my guess.