Trying to figure out why when writing to file, the last character written is being duplicated when cout looks fine on display - fstream

Question = Trying to figure out why the write to file for
myfile.open ("Crypt103Data.txt", ios::out | ios::app);
myfile <<tab2[code1];
myfile.close();
is writing the last character twice to Crypt103Data.txt when the cout which also runs for the same iterations within the while loop displays proper without the extra character write appended to file?
If you enter something like Hello_World you might see something like r$cc7fq7UcX depending on what seed value to entered which gives the output a unique output that is dependent on the seed key. While r$cc7fq7UcX is observed correctly on the screen below the Hello_World , what is written to file is r$cc7fq7UcXX where the last character is written twice and it should only be r$cc7fq7UcX . Not sure why this happens when the cout print to display and file write appended happens for the same iterations within the while loop. Instead of placing a band aid in to trim the last character from the file after each line is written, there must be a fix for this odd bug that someone here might be able to point out. Maybe I am using sloppy programming that is causing for this to occur. I am using the Bloodshed Dev c++ 4.9.9.2 IDE btw
When decrypted, due to this extra character write of the last character the output duplicated, it is decrypted as Hello_Worldd which is the problem.
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
// 12/30/2015 - Added string random shuffle
// 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key
// 1/4/2016 - Added ability to enter string and have while loop process v1.00
// 1/5/2016 - Added characters , and ; to supported characters which were missing from v1.01
// 1/8/2015 - Source code trash cleanup & offset band aid removed for IF statement logic +1 array offset v1.02
// 1/8/2015 - Added write to file for coded data storage eliminating need to copy/paste output to a text file v1.03
using namespace std;
int main(int argc, char *argv[])
{
int seed1=0;
int code1=0;
int run=1;
int again=1;
int test=1;
char ch1;
char ch2;
while(again==1){
cout<<" Crypt Version 1.03\n\n";
cout<<"XXXXXXXXXXXXXXXXXXXXX\n";
cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed
cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n";
cin >> seed1; // Input user seed
cout<<"\n\n"<<"Crypt String Key =\n\n";
ofstream myfile;
myfile.open ("Crypt103Key.txt", ios::out | ios::app);
myfile <<seed1<<"\n";
myfile.close();
//Initialize Valid Characters for String Shuffle Output
//Note Bug corrected with blank space for \ by use of escape character proceeding
string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!##$%^&*()_-+=?<>:\\/~.,;";
srand(seed1); // Allows user custom seeded starting algorithm position for random
random_shuffle(str.begin(), str.end()); // Shuffle the string
cout << str << "\n\n\n"; // Output the shuffle sequence
//Pass String Output into an array to pair up pointer value with associated character
string tmp = str; //Pass str output to string tmp
char tab2[128]; // Memory Allocation for array population
strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array
tab2[sizeof(tab2) - 1] = 0;
cout<<"Enter Info to Crypt in correct case\n";
cout<<"To Exit Inner Program enter ( ` ) \n\n\n";
while(run==1){
cin>>ch1;
if (ch1=='A'){
code1=0;
}
else if (ch1=='B'){
code1=1;
}
else if (ch1=='C'){
code1=2;
}
else if (ch1=='D'){
code1=3;
}
else if (ch1=='E'){
code1=4;
}
else if (ch1=='F'){
code1=5;
}
else if (ch1=='G'){
code1=6;
}
else if (ch1=='H'){
code1=7;
}
else if (ch1=='I'){
code1=8;
}
else if (ch1=='J'){
code1=9;
}
else if (ch1=='K'){
code1=10;
}
else if (ch1=='L'){
code1=11;
}
else if (ch1=='M'){
code1=12;
}
else if (ch1=='N'){
code1=13;
}
else if (ch1=='O'){
code1=14;
}
else if (ch1=='P'){
code1=15;
}
else if (ch1=='Q'){
code1=16;
}
else if (ch1=='R'){
code1=17;
}
else if (ch1=='S'){
code1=18;
}
else if (ch1=='T'){
code1=19;
}
else if (ch1=='U'){
code1=20;
}
else if (ch1=='V'){
code1=21;
}
else if (ch1=='W'){
code1=22;
}
else if (ch1=='X'){
code1=23;
}
else if (ch1=='Y'){
code1=24;
}
else if (ch1=='Z'){
code1=25;
}
else if (ch1=='a'){
code1=26;
}
else if (ch1=='b'){
code1=27;
}
else if (ch1=='c'){
code1=28;
}
else if (ch1=='d'){
code1=29;
}
else if (ch1=='e'){
code1=30;
}
else if (ch1=='f'){
code1=31;
}
else if (ch1=='g'){
code1=32;
}
else if (ch1=='h'){
code1=33;
}
else if (ch1=='i'){
code1=34;
}
else if (ch1=='j'){
code1=35;
}
else if (ch1=='k'){
code1=36;
}
else if (ch1=='l'){
code1=37;
}
else if (ch1=='m'){
code1=38;
}
else if (ch1=='n'){
code1=39;
}
else if (ch1=='o'){
code1=40;
}
else if (ch1=='p'){
code1=41;
}
else if (ch1=='q'){
code1=42;
}
else if (ch1=='r'){
code1=43;
}
else if (ch1=='s'){
code1=44;
}
else if (ch1=='t'){
code1=45;
}
else if (ch1=='u'){
code1=46;
}
else if (ch1=='v'){
code1=47;
}
else if (ch1=='w'){
code1=48;
}
else if (ch1=='x'){
code1=49;
}
else if (ch1=='y'){
code1=50;
}
else if (ch1=='z'){
code1=51;
}
else if (ch1=='1'){
code1=52;
}
else if (ch1=='2'){
code1=53;
}
else if (ch1=='3'){
code1=54;
}
else if (ch1=='4'){
code1=55;
}
else if (ch1=='5'){
code1=56;
}
else if (ch1=='6'){
code1=57;
}
else if (ch1=='7'){
code1=58;
}
else if (ch1=='8'){
code1=59;
}
else if (ch1=='9'){
code1=60;
}
else if (ch1=='0'){
code1=61;
}
else if (ch1=='!'){
code1=62;
}
else if (ch1=='#'){
code1=63;
}
else if (ch1=='#'){
code1=64;
}
else if (ch1=='$'){
code1=65;
}
else if (ch1=='%'){
code1=66;
}
else if (ch1=='^'){
code1=67;
}
else if (ch1=='&'){
code1=68;
}
else if (ch1=='*'){
code1=69;
}
else if (ch1=='('){
code1=70;
}
else if (ch1==')'){
code1=71;
}
else if (ch1=='_'){
code1=72;
}
else if (ch1=='-'){
code1=73;
}
else if (ch1=='+'){
code1=74;
}
else if (ch1=='='){
code1=75;
}
else if (ch1=='?'){
code1=76;
}
else if (ch1=='<'){
code1=77;
}
else if (ch1=='>'){
code1=78;
}
else if (ch1==':'){
code1=79;
}
else if (ch1=='\\'){ // Escape Character \ needed to allow \ check
code1=80;
}
else if (ch1=='/'){
code1=81;
}
else if (ch1=='~'){
code1=82;
}
else if (ch1=='.'){
code1=83;
}
else if (ch1==','){
code1=84;
}
else if (ch1==';'){
code1=85;
}
else if (ch1=='\`'){ //Escape Character \ before ` to exit
run=0; //Run = False at 0 and leaves while loop
}
else {
cout<<"Invalid Input = No Match\n\n";
}
cout<<tab2[code1];
myfile.open ("Crypt103Data.txt", ios::out | ios::app);
myfile <<tab2[code1];
myfile.close();
}// end inner while loop
//Add line return in file to seperate the data per line
myfile.open ("Crypt103Data.txt", ios::out | ios::app);
myfile <<" "<<"\n";
myfile.close();
test=1;
while(test==1){
system("CLS");
cout<<"Enter Y to continue or N to end \n\n\n";
cin>>ch2;
if (ch2=='N'||ch2=='n'){
again=0;
test=0;
}
else if (ch2=='Y'||ch2=='y'){
again=1;
test=0;
run=1;
system("CLS");
}
else {
cout<<"Invalid Input, please choose Y or N \n\n";
test=1;
}
}
} // end outter while loop
system("CLS");
system("PAUSE");
return EXIT_SUCCESS;
}

Related

Reading unread emails with selenium

i have the below code to read emails and check if the subject is matching with the expected message
Message[] messages = folder.getMessages();
String message="Thanks for contacting";
if(folder.getUnreadMessageCount()!=0)
{
for (Message mail : messages)
{
if(!mail.isSet(Flags.Flag.SEEN) && mail.getSubject().contains("Thanks"))//if mail is unread and the message matches
{
mail.setFlag(Flags.Flag.SEEN, true);
softAssert.assertTrue(true,"Email received ->");
Reporter.log("Email received ->" + mail.getSubject(), true);
break;
}
if(!mail.isSet(Flags.Flag.SEEN) && !mail.getSubject().contains("Thanks"))//if mail is unread and the message does not match
{
System.out.println(mail.getSubject() + "-> is not the email we are looking for");
}
}
}
else
{
softAssert.assertTrue(false,"Email not received");
Reporter.log("Email not received ->" + message, true);
}
The problem is I want to fail this test if both the conditions inside the for loop fail. if i put the else inside the for loop it prints not received for reach element in the loop. how do i go about this?
You can use boolean expression. See the below simple example,
boolean test = true;
boolean test2 = true;
for (int i = 0; i < 3; i++) {
if (!test) {
System.out.println("Test value is false.");
} else {
test = false;
}
if (!test2) {
System.out.println("Test2 value is false.");
} else {
test2 = false;
}
if(!test && !test2) {
System.out.println("Breaking loop.");
break;
}
}
Create two boolean variables and set their values as true incase if your if condition fails inside the loop then by using else block change the boolean value to false and do the same for your second if condition as well. In the last if condition if both statements fails then break the loop.

How can I write code to receive whole string from a device on rs232?

I want to know how to write code which receives specific string.for example, this one OK , in this I only need "OK" string.
Another string is also like OK
I have written code in keil c51 for at89s52 microcontroller which works but I need more reliable code.
I'm using interrupt for rx data from rs232 serial.
void _esp8266_getch() interrupt 4 //UART Rx.{
if(TI){
TI=0;
xmit_bit=0;
return ;
}
else
{
count=0;
do
{
while(RI==0);
rx_buff=SBUF;
if(rx_buff==rx_data1) //rx_data1 = 0X0D /CR
{
RI=0;
while(RI==0);
rx_buff=SBUF;
if(rx_buff==rx_data2) // rx_data2 = 0x0A /LF
{
RI=0;
data_in_buffer=1;
if(loop_conti==1)
{
if(rec_bit_flag==1)
{
data_in_buffer=0;
loop_conti=0;
}
}
}
}
else
{
if(data_in_buffer==1)
{
received[count]=rx_buff; //my buffer in which storing string
rec_bit_flag=1;
count++;
loop_conti=1;
RI=0;
}
else
{
loop_conti=0;
rec_bit_flag=0;
RI=0;
}
}
}
while(loop_conti==1);
}
rx_buff=0;
}
This is one is just for reference, you need develop the logic further to your needs. Moreover, design is depends on what value is received, is there any specific pattern and many more parameter. And this is not a tested code, I tried to give my idea on design, with this disclaimer here is the sample..
//Assuming you get - "OK<CR><LF>" in which <CR><LF> indicates the end of string steam
int nCount =0;
int received[2][BUF_SIZE]; //used only 2 buffers, you can use more than 2, depending on how speed
//you receive and how fast you process it
int pingpong =0;
bool bRecFlag = FALSE;
int nNofbytes = 0;
void _esp8266_getch() interrupt 4 //UART Rx.
{
if(TI){
TI=0;
xmit_bit=0;
return ;
}
if(RI) // Rx interrupt
{
received[pingpong][nCount]=SBUF;
RI =0;
if(nCount > 0)
{
// check if you receive end of stream value
if(received[pingpong][nCount-1] == 0x0D) && (received[pingpong][nCount] == 0x0A))
{
bRecFlag = TRUE;
pingpong = (pingpong == 0);
nNofbytes = nCount;
nCount = 0;
return;
}
}
nCount++;
}
return;
}
int main()
{
// other stuff
while(1)
{
// other stuff
if(bRecFlag) //String is completely received
{
buftouse = pingpong ? 0 : 1; // when pingpong is 1, buff 0 will have last complete string
// when pingpong is 0, buff 1 will have last complete string
// copy to other buffer or do action on received[buftouse][]
bRecFlag = false;
}
// other stuff
}
}

I am getting an error (variable y might not have been initialised in the if's in the loop. How to solve that?

import java.util.Scanner;
public class RPS
{
public void show()
{
int i;
System.out.println("1 - Rock 2 - Paper 3 - Scissor");
Scanner in = new Scanner(System.in);
i = in.nextInt();
double x = Math.random();
int y;
if(x<=0.33)
{
y=1;
}
else if(x>0.33 && x<0.67)
{
y=2;
}
else if(x>=0.67)
{
y=3;
}
for(;;)
{
if(i==y)
System.out.println("It's a draw!");
else if(i==1 && y==2)
System.out.println("Computer wins!");
else if(i==1 && y==3)
System.out.println("You win!");
else if(i==2 && y==1)
System.out.println("You win!");
else if(i==2 && y==3)
System.out.println("Computer wins!");
else if(i==3 && y==1)
System.out.println("Computer wins!");
else if(i==3 && y==2)
System.out.println("You win!");
else
System.out.println("Error!");
}
}
}
Whats wrong?
It gives an error that variable y might not have been intialised in the if's in the for loop.
I have assigned a value to y in the previous if-else section.
so why isnt it getting intialised?
javac is not smart enough to realize that the way your conditions are constructed, one of them will always be true.
You can rewrite your if-statements to make javac realize one branch will always be triggered:
int y;
if(x<=0.33)
{
y=1;
}
else if(x>0.33 && x<0.67)
{
y=2;
}
else // This only triggers when x >= 0.67, so no need to check
{
y=3;
}
Now javac sees that if the first two don't trigger, the last will, so y will always have a value.
You can alternatively add an else branch with an error, in case someone breaks the conditions:
int y;
if(x<=0.33)
{
y=1;
}
else if(x>0.33 && x<0.67)
{
y=2;
}
else if(x >= 0.67)
{
y=3;
}
else
{
// This should never happen
throw new IllegalArgumentException("Something's gone terribly wrong for " + x);
}
This also compiles, and if someone later decides to skew the numbers and turns the first condition into x <= 0.2 but forgets to update the other condition, you'll get an exception at runtime.

How to switch cases with ( - ) and ( + ) keys presses?

I am working with AutoHotKey. I know I have tagged C also, I think someone with enough C programming knowledge can also help here.
Code below is working for me.
It will read two keyboard input from user and based on what user pressed it will run code for that case.
1::
Input Key, L1
if Key=1
{
;your code
}
if Key=2
{
;your code
}
2::
Input Key, L1
if Key=1
{
;your code
}
if Key=2
{
;your code
}
I would like to know if I can add a loop or something if user presses + or - key it will go do one case at a time,
for example if user presses + for first time it will do
1 1 if user presses + again it will do
1 2 if user presses - it will do
1 1
and so on.
I am not sure if this is do able or not.
I am new to programming. please help :)
You can use global variables. A global variable can be accessed anywhere in the program, unlike a normal variable which exists only inside the function.
Example:
#NoEnv
#Persistent
SetBatchLines, -1
global myVar = 0
h::
myVar := myVar + 1
execute()
return
g::
myVar := myVar - 1
execute()
return
execute()
{
if(myVar == 1)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 2)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 3)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 4)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 5)
{
;do stuff
tooltip, myVar: %myVar%
}
else
{
; nothing
tooltip,
}
return
}
I hope this is what you were asking, i wasn't quite sure from the question.
; Some of this is what's called Pseudo code. (not sure if you're familiar). It gives you needs to be turned into actual code...
; Written for AHK...
CurrentNumber = 1
(plus key)::
CurrentNumber += 1
send %CurrentNumber%
return
(minus key)::
CurrentNumber -= 1
send %CurrentNumber%
return
; Not sure if this is what you were looking for or not.. if you want a loop it will be different.
; either way, good luck to you, i'm out..
+::
keywait, +, u
{
If var =
var = 11
Else
var++
}
Return
-::
keywait, -, u
{
If var =
var = 11
Else
var--
}
Return
"var" should have same name with the variable, which has two or one digit number, in your code.
You may use this too
NumpadAdd::
keywait, NumpadAdd, u
{
If var =
var = 11
Else
var++
}
Return
NumpadSub::
keywait, NumpadSub, u
{
If var =
var = 11
Else
var--
}
Return

if statement obj-c zero input

here is code.
int main(int argc, const char * argv[])
{
#autoreleasepool {
int x,y;
//BOOL divsibleYESOrNo;
NSLog(#"enter two number for test it\n");
scanf("%i %i",&x,&y);
if ( x%y ==0 ) {
NSLog(#"YES,it can be");
}
else if (x%y !=0) {
NSLog(#"no there cant.");
}
else
NSLog(#"zero is not allow");
}
return 0;
}
this code can not detect if the user input two zero.
how can I modify this code that can detect the input values are zero?
Thanks
Anything % 0 is undefined, so you'll need to add a check for y == 0 before your other if statements. It has to be the first if statement in order to make sure it gets evaluated; otherwise one of your other statements will catch it erroneously first.
if ( y == 0 ) {
NSLog(#"zero is not allowed.");
}
else if ( x%y == 0 ) {
NSLog(#"YES,it can be");
}
else {
NSLog(#"no there cant.");
}