Compiles in turbo c++. Not in visual studio [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 4 years ago.
Improve this question
I always get this error:
"Severity Code Description Project File Line Suppression State
Error C2371 'FILE': redefinition; different basic types ConsoleApplication2 c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\corecrt_wstdio.h 28
"
Why?
It used to compile in TurboC++.
Not in Visual Studio.
#include "stdio.h"
#include "string.h"
#include "iostream.h"
#include "conio.h"
class ari
{ public:
// int a;
//int b;
//int c;
int add (int a,int b)
{
return a+b;
}
int sub (int a,int b)
{
return a-b;
}
int mul (int a, int b)
{
return a*b;
}
int div (int a, int b)
{
return a/b;
}
};
class log
{ public:
// int a,b;
int gre (int a, int b)
{
if (a>b)
return a;
else
return b;
}
int sma (int a, int b)
{
if (a>b)
return b;
else
return a;
}
};
class gra
{ public:
//int w, b, c, k,a;
void unline()
{
cout << "\n\n";
for (int k=0 ; k<80 ; cout << "_", k++);
cout << "\n\n";
}
void wait (int w)
{
//cout << "\n\n";
for (long long long long int a=0 ; a<(w*100000) ; a++ );
//cout << "\n\n";
}
void asline()
{
cout << "\n\n";
for (int k=0 ; k<80 ; cout << "*", k++);
cout << "\n\n";
}
void hiline()
{
cout << "\n\n";
for (int k=0 ; k<80 ; cout << "-", k++);
cout << "\n\n";
}
void ctc()
{
hiline();
printf ("\n\nClick to continue.......... \n\n");
hiline();
getch();
clrscr();
}
};
void main()
{
gra gra;
ari ari;
log log;
FILE *p,*u,*r,*ra;
clrscr();
p=fopen("c://turboc3//bin//project//Password.txt","r+");
ra=fopen("c://turboc3//bin//project//Recans.txt","r+");
r=fopen("c://turboc3//bin//project//Recovery.txt","r+");
u=fopen("c://turboc3//bin//project//Username.txt","r+");
char pas[44], user[44], reco[88] , recoa[44];
devsta:
fseek(p, 0 , 0);
fscanf(p,"%s", &pas);
fseek(u, 0 , 0);
fscanf(u,"%s", &user);
fseek(r, 0 , 0);
fscanf(r,"%s", &reco);
fseek(ra, 0 , 0);
fscanf(ra,"%s", &recoa);
fseek(ra, 0 , 0);
fseek(p, 0 , 0);
fseek(u, 0 , 0);
fseek(r, 0 , 0);
printf("%s\n", user);
printf("%s\n", pas);
printf("%s\n", reco);
printf("%s\n", recoa);
getch();
int a;
gra.hiline();
printf ("\t\t\t\t Welcome to\n\t\t\t\tLogin Screen!!\n\n");
gra.asline();
printf ("Click to continue.......... \n\n");
gra.hiline();
getch();
clrscr();
start:
printf ("What you want to do?\n1-Login\n2-Reset using recovery question?\n\n");
scanf("%d", &a);
char inpass[44], inuser[44];
if (a==1)
{
start2:
gra.ctc();
printf("\n\nEnter username : ");
scanf ("%s", &inuser);
printf("\n\nEnter password : ");
scanf ("%s", &inpass);
if (strcmp(inuser,user) && strcmp(inpass,pas))
{
printf("Wrong!!");
gra.ctc();
goto start2;
}
else
{
stay:
gra.ctc();
printf("\t\t\tWelcome\nYou have entered the correct credtionals!!\n");
printf("1-Log off\n2-Change password\n\n");
int i;
scanf("%d", &i);
if (i==1)
{
gra.ctc();
goto logoff;
}
else if (i==2)
{
start3:
gra.ctc();
printf("\n\nEnter username : ");
scanf ("%s", &inuser);
printf("\n\nEnter password : ");
scanf ("%s", &inpass);
if (strcmp(inuser,user) && strcmp(inpass,pas))
{
printf("Wrong!!");
gra.ctc();
printf("Wish to cont\n1-Yes\n2-No");
int y;
scanf("%d", &y);
if (y==1)
goto start3;
else
goto logoff;
}
else
{
char newpass[44], newuser[44], cnewpass[44], newreco[88], newreca[88];
start4:
printf ("\nEnter new username : ");
scanf("%s", &newuser );
//printf("\nEnter new password : ");
printf ("\nEnter new recovery question : ");
scanf("%s", &newreco);
printf("\nEnter new recovery answer : ");
scanf("%s", &newreca);
printf ("\nEnter new password : ");
scanf("%s", &newpass);
printf("\nConfirm new password : ");
scanf("%s", &cnewpass);
if (strcmp(newpass, cnewpass))
{
printf("Passwords dont match\n");
gra.ctc();
goto start;
}
else
{
fclose(p);
fclose(u);
fclose(ra);
fclose(r);
p=fopen("c://turboc3//bin//project//Password.txt","w");
ra=fopen("c://turboc3//bin//project//Recans.txt","w");
r=fopen("c://turboc3//bin//project//Recovery.txt","w");
u=fopen("c://turboc3//bin//project//Username.txt","w");
printf("\n\nPassword and username successfully changed.....\n\n\n");
fseek(p, 0 , 0);
fprintf(p,"%s", newpass );
cout << "\nConfirmations: ";
printf ("\nNew password : %s", newpass);
fseek(u, 0 , 0);
fprintf(u,"%s",newuser );
printf ("\nNew username : %s", newuser);
fseek(r, 0 , 0);
fprintf(r,"%s", newreco);
printf ("\nNew recovery question : %s", newreco);
fseek(ra, 0 , 0);
fprintf(ra,"%s", newreca);
printf ("\nNew recovery answer : %s", newreca);
cout << "\nAll settings are saved....";
cout << "\nRedirecting .....";
gra.ctc();
fclose(p);
fclose(u);
fclose(ra);
fclose(r);
p=fopen("c://turboc3//bin//project//Password.txt","a+");
ra=fopen("c://turboc3//bin//project//Recans.txt","a+");
r=fopen("c://turboc3//bin//project//Recovery.txt","a+");
u=fopen("c://turboc3//bin//project//Username.txt","a+");
goto devsta;
}
}
}
}
}
else if (a==2)
{
char enterrecoa[88];
printf("\n\nRecovery question\n%s\nEnter recovery answer....\nDefault answer nil,\nQuestion nil\n", reco);
scanf ("%s", &enterrecoa);
if (strcmp(enterrecoa, recoa))
{
printf ("\n\nIncorrect!");
gra.ctc();
goto devsta;
}
else
{
fclose(p);
fclose(u);
fclose(ra);
fclose(r);
p=fopen("c://turboc3//bin//project//Password.txt","w");
ra=fopen("c://turboc3//bin//project//Recans.txt","w");
r=fopen("c://turboc3//bin//project//Recovery.txt","w");
u=fopen("c://turboc3//bin//project//Username.txt","w");
fseek(ra, 0 , 0);
fseek(p, 0 , 0);
fseek(u, 0 , 0);
fseek(r, 0 , 0);
fseek(ra, 0 , 0);
fseek(p, 0 , 0);
fseek(u, 0 , 0);
fseek(r, 0 , 0);
fprintf(p,"admin");
fprintf(u,"admin");
fprintf(r,"nil");
fprintf(ra,"nil");
printf("All user settings are back to default");
fseek(ra, 0 , 0);
fseek(p, 0 , 0);
fseek(u, 0 , 0);
fseek(r, 0 , 0);
fclose(p);
fclose(u);
fclose(ra);
fclose(r);
p=fopen("c://turboc3//bin//project//Password.txt","a+");
ra=fopen("c://turboc3//bin//project//Recans.txt","a+");
r=fopen("c://turboc3//bin//project//Recovery.txt","a+");
u=fopen("c://turboc3//bin//project//Username.txt","a+");
gra.ctc();
goto devsta;
}
}
logoff:
printf("exit");
getch();
fclose(p);
fclose(u);
fclose(ra);
fclose(r);
}

1) Change the beginning of your file from this:
#include "stdio.h"
#include "string.h"
#include "iostream.h"
#include "conio.h"
to this:
#include <cstdio>
#include <cstring>
#include <iostream>
#include "conio.h"
using namespace std;
2) Rename the class log to mylog, log is a standard function already defined via #include <iostream>.
3) Remove the calls to clrscr(), there is no such thing anymore.
4) Replace for (long long long long int a = 0; a<(w * 100000); a++); with for (long int a = 0; a<(w * 100000); a++);. What is this supposed to do? I doubt that this compiled with TurboC++
Then it will compile without errors.

Change your includes to refere to standard C++ Headers:
#include <cstdio>
#include <cstring>
#include <iostream>
and either use
using namespace std;
or prepend symbols from the standard library with std::.

Related

caret in mvprintw but only in loop

I have the following program
#include <ncurses.h>
int main() {
initscr();
const char c = static_cast<char>(65);
mvprintw(0, 0, "%s", &c);
getch();
endwin();
return 0;
}
it prints simple "A" and waits.
If I modifying "x" argument of mvprintw
mvprintw(0, 1, "%s", &c);
it will print "A" with empty space prepend on beginning.
If I will add for loop starting from 0 it also works as expected
int main() {
initscr();
for (int i = 0; i < 1; i++) {
const char c = static_cast<char>(65);
mvprintw(0, i, "%s", &c);
}
getch();
endwin();
return 0;
}
will show the same result as in first example.
But if this loop starts from 1 there is stragne ^A at the end.
This code:
int main() {
initscr();
for (int i = 1; i < 2; i++) {
const char c = static_cast<char>(65);
mvprintw(0, i, "%s", &c);
}
getch();
endwin();
return 0;
}
produces
and I even do not know how to debug it?
If mvprintw is anything like printf then you are supposed to provide a nul terminated string when you use %s. Try this instead
const char c[] = { static_cast<char>(65), '\0' };
mvprintw(0, i, "%s", c);
Note in this version c not &c.

How to add statement if-else?

How to add if-else statement for "kodeprodi"?
Everytime I add if-else statement, the message "Lvalue required" always appears.
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct {
char bp[13];
char nama[15];
int kodeprodi;
char namaprodi[10];
float ipk;
} mahasiswa;
int main()
{
char pil;
do {
mahasiswa mhs[10];
int i, n;
{
printf("Data Nilai Mahasiswa\n");
printf("Berapa banyak data = ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Data mahasiswa ke-%d\n", i+1);
printf("Nomor BP: "); scanf("%s", &mhs[i].bp);
printf("Nama: "); scanf("%s", &mhs[i].nama);
printf("Kode Prodi: "); scanf("%d", &mhs[i].kodeprodi);
printf("IPK: "); scanf("%f", &mhs[i].ipk);
if (mhs[i].kodeprodi == 260) {mhs[i].namaprodi = "SI";}
else if (mhs[i].kodeprodi == 261) {mhs[i].namaprodi = "TI";}
}
//output
printf("No. BP Nama Kode Prodi Nama Prodi IPK \n");
for(i = 0; i < n; i++) {
printf("\n%2d %-10s %-9s %3d %3s %3.f\n",
i+1, mhs[i].bp, mhs[i].nama, mhs[i].nama,
mhs[i].kodeprodi, mhs[i].namaprodi, mhs[i].ipk);
}
}
printf("Repeat again? Y/N");
scanf("%s", &pil);
printf("\n\n");
} while ((pil == 'Y') || (pil == 'y'));
}
Even if in the statement if-else, I type like this
if(mhs[i].kodeprodi==260){namaprodi = "SI");
The error message is "Undefined symbol 'namaprodi'
I tweaked your code a bit. Got rid of unused conio.h, changed kodeprodi type to int (because char can only handle numbers -127..127), removed & from some scanf calls (because you should pass pointer to first character for %s formatter), deleted extra mhs[i].nama argument for printf.
Sorry, I completely didn't understood your code :-) My tweaks were semi-automatic! You should learn C programming better.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char bp[13];
char nama[15];
int kodeprodi;
char namaprodi[10];
float ipk;
} mahasiswa;
int main() {
char pil;
do {
mahasiswa mhs[10];
int i, n;
{
printf("Data Nilai Mahasiswa\n");
printf("Berapa banyak data = ");
scanf("%d", &n);
for(i=0;i<n;i++) {
printf("Data mahasiswa ke-%d\n", i+1);
printf("Nomor BP: "); scanf("%s", mhs[i].bp);
printf("Nama: "); scanf("%s", mhs[i].nama);
printf("Kode Prodi: "); scanf("%d", &mhs[i].kodeprodi);
printf("IPK: "); scanf("%f", &mhs[i].ipk);
if(mhs[i].kodeprodi==260)
strcpy(mhs[i].namaprodi, "SI");
else if(mhs[i].kodeprodi==261)
strcpy(mhs[i].namaprodi, "TI");
}
//output
printf("No. BP Nama Kode Prodi Nama Prodi IPK \n");
for(i=0;i<n;i++) {
printf("\n%2d %-10s %-9s %3d %3s %3.f\n", i+1, mhs[i].bp, mhs[i].nama, mhs[i].kodeprodi, mhs[i].namaprodi, mhs[i].ipk);
}
}
printf("Repeat again? Y/N");
scanf("%s", &pil);
printf("\n\n");
} while ((pil == 'Y') || (pil == 'y'));
return 0;
}
For a quick fix, use:
if(mhs[i].kodeprodi==260){strncpy(mhs[i].namaprodi, "SI", 9);
strncpy() is needed to copy the contents into namaprodi.
namaprodi is a member of struct mahasiswa, so you can't access it directly.
But better use std::string instead.
Also, as #BoPersson mentioned, char kodeprodi; can't hold 260, so you'll better to convert that to an int.

Web Browser Turbo C++

So I was browsing the Internet for a project and stumbled upon this piece of code. It runs perfectly until the point you enter a website(include "www" :P) and press Enter and then boom! The program terminates, no error message, nothing.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <dos.h>
#include <string.h>
void initialize_graphics_mode();
int get_key();
void draw();
union REGS i, o;
main()
{
int key, i = 0, xpos, ypos, button;
char arr[200], temp[5], *ptr;
char a[] = "C:\\Progra~1\\Mozill~1\\firefox ";
strcpy(arr,a);
i = strlen(a);
initialize_graphics_mode();
draw();
while(1)
{
if(kbhit())
key = get_key();
if((key>=97&&key<=122)||(key>=65&&key<=90)||key==46||key==47||key==63)
{
arr[i] = key;
sprintf(temp,"%c",arr[i]);
outtext(temp);
if(getx()>470)
{
clearviewport();
moveto(5,2);
}
i++;
}
else if ( key == 13 )
{
arr[i] = '\0';
system(arr);
break;
}
else if ( key == 27 )
{
closegraph();
exit(EXIT_SUCCESS);
}
if(button==1&&xpos>=150&&xpos<=480&&ypos>=300&&ypos<=330)
{
system("C:\\Progra~1\\Mozill~1\\firefox programmingsimplified.com");
break;
}
key = -1;
}
closegraph();
return 0;
}
void initialize_graphics_mode()
{
int gd = DETECT, gm, errorcode;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
errorcode = graphresult();
if( errorcode != grOk )
{
printf("Graphics error : %s\n",grapherrormsg(errorcode));
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
}
int get_key()
{
i.h.ah = 0;
int86(22,&i,&o);
return( o.h.al );
}
void draw()
{
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(275,11,"Web Browser");
outtextxy(155,451,"<a> href=http://www.programmingsimplified.com"">www.programmingsimplified.com</a>");
outtextxy(5,105,"Enter URL : ");
rectangle(120,100,600,130);
setviewport(121,101,599,129,1);
moveto(5,1);
}
Could someone help me in understanding why this happens? and possibly a fix for this?
NOTE: + It is supposed to work if you have Mozilla Firefox. If you have any other browser, please edit the code accordingly.
I am running this on Turbo C++, what would I have to do in order to convert this code for Dev-C++?
Thanks in advance.
P.S: I'm new to C++ so elaborate as much as possible.

Using 'fgets' to read in a char is just skipping when running

the issue is in the read_in function when i use fgets to read in the users input for a band/singer it will just skip this line. I cant work out why this is happening? I did use scanf for this before but the issue is that if you do that when you enter a band name with a space e.g 'foo fighters' it will skip the next print name of reference line. Anyone know how to fix this?
#include <stdio.h>
#include <conio.h>
#define MAX 1000`enter code here`
int exit = 0; // Declaring a global variable
struct data { // using struct to put the data in an arrays
char band [48], cd [48], ref [16];
float cost;
int year;
} cata [MAX]; // declaring a struct variable
int read_in ( void )
{
int i = 0;
printf("\n");
while ( exit != 2 ) // while exit != 2 then ask the user to enter the information
{
printf("\nPlease Enter the Name of Band/Singer: ");
fgets(cata[i].band, 48, stdin);
printf("\nPlease Enter the Name of CD: ");
scanf("%s", &cata[i].cd);
printf("\nPlease Enter the Name of the Reference: ");
scanf("%s", &cata[i].ref);
printf("\nPlease Enter the cost: ");
scanf("%f", &cata[i].cost);
printf("\nPlease Enter the Year of Release: ");
scanf("%d", &cata[i].year);
printf("\nWrite another CD: [1] \nMain Menu: [2]\nYour choice: "); // Asking him a choice either write another one or go back to menu
scanf("%d", &exit);
i++;
}
exit = 0;
return i;
}
void print_out (void)
{
FILE *file_cata;
int i = 0;
printf("\n");
while ( exit != 2 )
{
file_cata = fopen ("catalogue.txt", "r"); // open the file and read
if (file_cata == NULL)
{
printf("Error: can't open files.\n");
}
else
{
while (!feof (file_cata))
{
fgets(cata[i].band, MAX , file_cata); // it will scanf the file and get the string and then print it out on screen
printf("%s", cata[i].band);
fgets(cata[i].cd, MAX, file_cata);
printf("%s", cata[i].cd);
fgets(cata[i].ref, MAX, file_cata);
printf("%s", cata[i].ref);
fscanf(file_cata, "%.2f" , cata[i].cost);
fscanf(file_cata, "%d", cata[i].year);
i++;
}
}
fclose (file_cata); // close file
printf("Read it again: [1] \nMain Menu: [2]\nYour choice: ");
scanf("%d", &exit);
}
exit = 0;
}
void save ( int num )
{
FILE *file_cata;
int i = 0;
printf("\n");
while ( exit != 2 )
{
file_cata = fopen ("catalogue.txt", "a"); // file append, so it will write at the end of the file
if (file_cata == NULL)
{
printf("Error: can't open files. \n");
}
else
{
while (i != num)
{
fprintf( file_cata, "\n");
fprintf( file_cata, "The name of Band/Singer: %s \n", cata[i].band);
fprintf( file_cata, "The name of CD: %s \n", cata[i].cd);
fprintf( file_cata, "The name of Reference: %s\n", cata[i].ref);
fprintf( file_cata, "The Cost: %.2f \n", cata[i].cost);
fprintf( file_cata, "The Year of Release: %d \n", cata[i].year);
i++;
}
}
fprintf( file_cata, "\n");
fclose (file_cata);
printf("Your data has been saved to the catalogue.\n\n");
printf("Save it again: [1] \nMain Menu: [2]\nYour Choice: ");
scanf("%d", &exit);
}
exit = 0;
}
void main ()
{
int num1 = 0;
int option = 0;
while ( option != 4 )
{
printf("The following options are availlable: \n");
printf("Read in data [1] \n");
printf("Print out catalogue to screen [2] \n");
printf("Save data to file [3] \n");
printf("Exit Program [4] \n");
printf("Enter your choice now: ");
scanf( "%d", &option);
if (option == 1)
num1 = read_in ();
if (option == 2)
print_out ();
if (option == 3)
save(num1);
printf("\n");
}
printf("\n*** Your program will end when you press ANY button. ***\n");
_getch();
}
The scanf is leaving the newline in the input buffer. Related question with a very good answer (Hint: don't use scanf; use fgets always, plus sscanf if you must convert that way.): Replacement of fflush(stdin)
This flushes the input buffer, which you should do before attempting to read another line (stolen from answer above):
while((c = getchar()) != '\n' && c != EOF)

X&0 Game IPC FIFO windows [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i want to make a x&0 game with ipc in c.i declare 3 char arrays,read the witch line to choose and the on what position to put the x or 0.when i try to send line number through the fifo from the client the server recieves a different number.for example i send 1 and the server gets 3144200....this is the code it's not all just one read and write.
this is the server:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
int main() {
HANDLE f1, f2;
DWORD x;
char l1[3]="\0";
char l2[3]="\0";
char l3[3]="\0";
char X='x';
char* a="";
char* lineChar="";
int n=1,lineInt=0,coor=0;
printf("I am the server \n");
printf("You will play with x\n");
// creating pipes
f1=CreateNamedPipe(TEXT("\\\\.\\PIPE\\fifo1"), PIPE_ACCESS_INBOUND,PIPE_TYPE_BYTE|PIPE_WAIT, 3, 0, 0, 0, NULL);
f2=CreateNamedPipe(TEXT("\\\\.\\PIPE\\fifo2"), PIPE_ACCESS_OUTBOUND,PIPE_TYPE_BYTE|PIPE_WAIT, 3, 0, 0, 0, NULL);
ConnectNamedPipe(f1, NULL);
ConnectNamedPipe(f2, NULL);
while(n<=9){
for(int i=0;i<=2;i++){
printf("[%s]",&l1[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l2[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l3[i]);
}
printf("\n");
printf("You will begin,select the line from 1 to 3 : \n");
scanf_s("%d",&lineInt);
x=0;
lineChar=reinterpret_cast<char*>(lineInt);
printf("%s",lineChar);
//strcpy_s(a,sizeof(lineChar),lineChar);
if (WriteFile(f2,lineChar,sizeof(lineChar)+1, &x, NULL)==0) {
printf("writing error..%d\n", x);
}
n++;
}
DisconnectNamedPipe(f1);
DisconnectNamedPipe(f2);
CloseHandle(f1);
CloseHandle(f2);
}
this is the client :
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main(){
HANDLE f1, f2;
DWORD x;
char l1[3]="\0";
char l2[3]="\0";
char l3[3]="\0";
char o='0';
char* lineChar="";
int n=1,lineInt=0,coor=0;
printf("I am the client \n");
printf("You will play with 0\n");
// connect to pipes created by server
f1=CreateFile(TEXT("\\\\.\\PIPE\\fifo1"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
f2=CreateFile(TEXT("\\\\.\\PIPE\\fifo2"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
while(n<=9){
for(int i=0;i<=2;i++){
printf("[%s]",&l1[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l2[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l3[i]);
}
printf("\n");
x=0;
Sleep(3000);
if (ReadFile(f2, lineChar, sizeof(lineChar), &x, NULL)==0) {
printf("reading error..%d\n", x);
}
printf("%s",lineChar);
//lineInt=atoi(lineChar);
//printf("\n%d\n",lineInt);
n++;
}
CloseHandle(f1);
CloseHandle(f2);
}
You have lots or problems with strings and pointers in your code.
To start with you print out the single characters in the arrays as strings, which will cause weird output.
Secondly, and one of the causes of your problem, when you call WriteFile you use sizeof(lineChar) which returns the size of the pointer, not the length of the string. Use strlen instead.
The second cause of your problem is that you cast an integer to a string. This will not work! What the statement
lineChar=reinterpret_cast<char*>(lineInt);
does is that it makes a pointer out of the value in lineInt. This is not a valid pointer! There are a couple of ways to do it:
Use the new std::to_string to convert a value to a std::string.
Since you mix C and C++ anyway, you could use sprintf too.
Your programs have a lot of what is called undefined behavior, and you should be happy that neither of them crashes outright.
#include<stdio.h>
#include<string.h>
int main(){
char l1[3]="\0";
char l2[3]="\0";
char l3[3]="\0";
char x='x';
char o='0';
int n=1,lineInt=0,coor=0,next=1;
printf("X & 0 Game : \n");
while(n<=3){
for(int i=0;i<=2;i++){
printf("[%s]",&l1[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l2[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l3[i]);
}
printf("\n");
switch (next){
case 1 :{
printf("To select the line enter 1,2 or 3 : \n");
scanf_s("%d",&lineInt);
switch (lineInt){
case 1 :{
printf("Enter the coordinates : \n");
scanf_s("%d",&coor);
//printf("%c",x);
//printf("%d",coor);
//strcpy_s(&l1[coor],1,"x");
l1[coor]=x;
break;
}
case 2 :{
printf("Enter the coordinates : \n");
scanf_s("%d",&coor);
l2[coor]=x;
break;
}
case 3 :{
printf("Enter the coordinates : \n");
scanf_s("%d",&coor);
l2[coor]=x;
break;
}
next=2;
}
for(int i=0;i<=2;i++){
printf("[%s]",&l1[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l2[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l3[i]);
}
printf("\n");
case 2 :{
printf("To select the line enter 1,2 or 3 : \n");
scanf_s("%d",&lineInt);
switch (lineInt){
case 1 :{
printf("Enter the coordinates : \n");
scanf_s("%d",&coor);
l1[coor]=o;
break;
}
case 2 :{
printf("Enter the coordinates : \n");
scanf_s("%d",&coor);
l2[coor]=o;
break;
}
case 3 :{
printf("Enter the coordinates : \n");
scanf_s("%d",&coor);
l2[coor]=o;
break;
}
next=2;
}
}
}
n++;
}
}
}