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.
I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf() and after scanf(). Althougth printf is written before scanf() the output differs. I was able to find out something about similar issue here. But I wasn't able to solve it. Any ideas?
Code:
#include <stdio.h>
int main()
{
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
Expected output:
Enter a number:1
1
Instead I get:
1
Enter a number:1
Your output is being buffered.
You have 4 options:
explicit flush
fflush after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.
fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
disable buffering in your console through what ever options menu it provides
Examples:
Here is your code with option 1:
#include <stdio.h>
int main() {
int myvariable;
printf("Enter a number:");
fflush( stdout );
scanf("%d", &myvariable);
printf("%d", myvariable);
fflush( stdout );
return 0;
}
Here is 2:
#include <stdio.h>
int main() {
int myvariable;
setlinebuf(stdout);
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
and 3:
#include <stdio.h>
int main() {
int myvariable;
setbuf(stdout, NULL);
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
Ok, so finally I used something similar to what #zsawyer wrote as an option labelled 3.
In my code I inserted this line:
setvbuf(stdout, NULL, _IONBF, 0);
As a first line in main():
#include <stdio.h>
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
I got it from here.
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::.
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.
I've been having a few problems with using an array of structs and simply listing them and adding to them. I am not really all too sure about what's happening, I've had a search around and asked a few friends and they've suggested that it is something to do with the memory allocation and another has said that memory allocation is not needed and that there is a problem in my code.
I have been unable to locate the problem and was wondering if anyone would be able to point me in the direction of to where I am going wrong.
I apologies if the code doesn't look right - not really sure on how to implement it on the site.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct data{
int testone;
int testtwo;
}data;
struct data _dataStore[25];
int dataCount = 0;
int addData(struct data __dataStore[dataCount]){
printf("\n\t\t\tPacket Source - ");
scanf("%i", &_dataStore[dataCount].testone);
printf("\t\t\tPacket Destination - ");
scanf("%i", &_dataStore[dataCount].testtwo);
system("CLS");
return 0;
}
void listData(struct data _dataStore[25]){
int i = 0;
for (i = 0; i < dataCount; i++){
printf("data stored - %i",dataCount);
printf("%i___%i \n", _dataStore[dataCount].testone,_dataStore[dataCount].testtwo);
}
}
int main(){
char choice;
do{
printf("\t\t\t Counter - %i", dataCount+1);
printf("\n\t\t\t1 - Add data. \n");
printf("\t\t\t2 - List data. \n");
printf("\n\t\t\tEnter your choice - ");
fflush(stdin);
choice = getchar();
getchar();
switch(choice){
case '1':
addData(&_dataStore[dataCount]);
dataCount++;
system("CLS");
break;
case '2':
system("CLS");
listData(&_dataStore[dataCount]);
break;
default:
printf("Invalid input \n");
break;
}
}while (choice != '5');
return 0;
}
There are quite a few apparent bugs, for example:
for (i = 0; i < dataCount; i++){
printf("data stored - %i",dataCount);
printf("%i___%i \n", _dataStore[dataCount].testone,_dataStore[dataCount].testtwo);
}
}
I suspect you wanted to say _dataStore[i] instead. Also, I suggest using a more sensible function prototype:
int addData(struct data* __dataStore, int arrayLen)
Here is the corrected code which works well for me:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct data{
int testone;
int testtwo;
}data;
struct data _dataStore[25];
int dataCount = 0;
int addData(){
printf("\n\t\t\tPacket Source - ");
scanf("%d", &_dataStore[dataCount].testone);
printf("\t\t\tPacket Destination - ");
scanf("%d", &_dataStore[dataCount].testtwo);
system("CLS");
return 0;
}
void listData(){
int i = 0;
for (i = 0; i < dataCount; i++){
printf("data stored - %d: ",dataCount);
printf("%d___%d \n", _dataStore[i].testone,_dataStore[i].testtwo);
}
}
int main()
{
char choice;
do{
printf("\t\t\t Counter - %i", dataCount+1);
printf("\n\t\t\t1 - Add data. \n");
printf("\t\t\t2 - List data. \n");
printf("\n\t\t\tEnter your choice - ");
fflush(stdin);
choice = getchar();
getchar();
switch(choice){
case '1':
addData();
dataCount++;
system("CLS");
break;
case '2':
system("CLS");
listData();
break;
case '5':
printf("Will exit...");
break;
default:
printf("Invalid input \n");
break;
}
}while (choice != '5');
return 0;
}
Please pay attention to the following: read / write integers with %d not %i, the index in listData() shall be i not dataCount, I removed the not-needed params of the functions since the variables are global, I added a new case in switch() so that it prints a better message when exiting, not "invalid input".
All variables you are using are statically allocated so you do not need memory allocation.