I am pretty new to c++ and ncurses, so sorry if my question is weird or obvious.
I managed to make a simple tui program that will do various tasks, my problem is that I have no back functionality or something that would loop the program.
I want to be able to go back to the main menu after accessing one of the submenus and I want to make the program repeat after it's done so I won't have to keep opening it over and over, so do you have any solutions that I can apply?
Thank you in advance
This is my code:
int main(int argc, char ** argv)
{
int i=0;
string input;
initscr();
noecho();
cbreak();
curs_set(0);
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax);
WINDOW * menuwin = newwin (0, xMax, 0, 0);
leaveok(menuwin, true);
refresh();
wrefresh(menuwin);
keypad(menuwin, true);
string choices[4] = {"Youtube","Music","Anime","Games"};
int choice;
int highlight = 0;
int option = 0;
while(1)
{
for (i=0;i < 4; i++)
{
if(i == highlight)
wattron(menuwin, A_REVERSE);
mvwprintw(menuwin, i+1, 1, choices[i].c_str());
wattroff(menuwin, A_REVERSE);
}
choice = wgetch(menuwin);
switch(choice)
{
case KEY_UP:
highlight--;
if(highlight == -1)
highlight = 3;
break;
case KEY_DOWN:
highlight++;
if(highlight == 4)
highlight = 0;
break;
default:
break;
}
if(choice == 10)
{
break;
}
}
werase(menuwin);
switch(highlight)
{
case 0:
{
menu2();
break;
}
case 1:
{
menu3();
break;
}
case 2:
{
menu4();
break;
}
case 3:
{
menu2();
}
default:
break;
}
endwin();
return 0;
}
The following program does not work properly. It is supposed to be a calculation game which should not loop over and over. I'm not exactly sure what's going on. I tried rearranging some things but that did not help. I noticed that after the user inputs the numbers and answers the question, it just goes to mainMenu even though it should go to display.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//Declaration Statement
double num1;
double num2;
double answer;
int choice;
void pauseProgram()
{
printf("\nPress any key to continue...");
getchar();
}
//Function Title
void title(char * programTitle)
{
int len = strlen(programTitle);
system("cls");
printf("\n");
for(int i=1; i<40 - len/2;i++) printf(" ");
printf("%s\n",programTitle);
}
void intro()
{
title("Calculation Game");
printf("\nThis program will test your math abilites");
pauseProgram();
}
void userInput()
{
title("Calculation Game");
printf("\nPlease enter positive numbers only\n");
printf("Enter a number:");
scanf("%lf",&num1);
getchar();
printf("Enter another number:");
scanf("%lf",&num2);
getchar();
printf("What is %lf + %lf?",num1,num2);
scanf("%lf",&answer);
if (num1<0 or num2<0)
{
printf("\nSorry, you must enter a positive value! Please try again.");
userInput();
pauseProgram();
}
}
void display()
{
title("Calculation Game");
if (answer=num1+num2)
printf("\nWow you got the right answer\n");
else if (answer!=num1+num2)
printf("\nHmmm...maybe we should review this math concept again!\n");
pauseProgram();
}
void goodbye()
{
title("Calculation Game");
printf("\nFor further information call: 1-800-123-4567\n");
pauseProgram();
}
void mainMenu()
{
title("Calculation Game");
printf("\n1.\tPlay Game");
printf("\n2.\tExit\n");
printf("\nEnter 1 or 2:");
scanf("%d",&choice);
getchar();
if (choice==1)
userInput();
if (choice=2)
goodbye();
if (choice!=1 or choice!=2);
{
printf("\nPlease enter either 1 or 2! Please try again.");
mainMenu();
}
}
//Main program
int main()
{
do
{
intro();
mainMenu();
userInput();
if (answer==num1+num2 or answer!=num1+num2)
{
display();
}
}while(1);
goodbye();
}
As I mentionned in my comment, you can probably see the main() function enters an infinite loop:
do {
...
} while (1)
This loop will not stop until told so. For instance, you can tell the program to exit in your pauseProgram() function :
void pauseProgram()
{
printf("\nPress any key to continue...");
getchar();
exit(0);
}
or in your goodbye() function :
void goodbye()
{
title("Calculation Game");
printf("\nGame designed by David Liubart\n");
printf("\nFor further information call: 1-800-123-4567\n");
pauseProgram();
exit(0);
}
Finally, you could use a break instruction anywhere in the loop in order to exit the loop.
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.
I am facing two errors for the below program:
On serial monitor it is not printing IN main().
When enter lcd_call() function. If any key being pressed it goes to next case. As per program it should stay in same window but it is going back to case 1.
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("In main");
lcd_call();
}
void lcd_call()
{
int menuOption=0;
int button;
do
{
switch(menuOption)
{
case 0:
Serial.println("set+date:");
Display_Date_Time();
timedBeep(shortBeep,1);
break;
case 1:
Serial.println("DISPLAY:");
lcdClear();
lcd.print("DISPLAY MENU");
timedBeep(shortBeep,1);
break;
case 2:
Serial.println("SET MENU:");
lcdClear();
lcd.print("SET MENU");
timedBeep(shortBeep,1);
break;
}
button=read_LCD_buttons();
if(button==btnRIGHT)
{
menuOption=menuOption+1;
if(menuOption>2)
{
menuOption=0;
}
}
else
{
menuOption;
}
}
while(menuOption<menuOptions);
}
I have programmed a code for displaying a GUI menu in Turbo C++ but I don't know why it wont recognize initgraph
This is the output message
BGI Error:Graphics not initialized
when i tried a simpler program on graphics in Turbo C++ it said egavga.bgi not found but it was there in the bgi file in my turbo c++ folder
Also when i tried to put the location of the bgi in initgraph it doesnt do anything
this is the program code
#include<iostream.h>
#include<conio.h>
#include <dos.h>
#include<graphics.h>
//Menu Global Item
#define pixTOrc(x) (8*(x-1)) //convert pixel into row and col format
#define INC 5 //Increment Distance Between Menu Items
#define ROW 15 //Row Value for Menu Item
#define COL 8 //Column Value for Menu Item
#define MAXMENU 5 //Total menu items
// To display the Inventory Main menu options
typedef char option[20];
option MMenu[]= {
"View Account",
"Transactions",
"New Accont",
"Edit Account",
"Quit"
};
// Function to displays all the menu prompt messages from the pointer array of option a[]
void normalvideo(int x,int y,char *str)
{
x=pixTOrc(x);
y=pixTOrc(y);
outtextxy(x,y,str);
}
// Function to move the cursor on the menu prompt with a reverse video color
void selectedMenu(int x,int y,char *str)
{
x=pixTOrc(x);
y=pixTOrc(y);
setcolor(5); //Selected Item Color
sound(400);
delay(100);
nosound();
outtextxy(x,y,str);
setcolor(WHITE); //Unselected Item Color
sound(500);
delay(100);
nosound();
}
//Keep Track of which arrow key is pressed
char menu()
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
setcolor(WHITE); //Initial Menu Item Color
int i,done;
for(i = 1; i < MAXMENU; i++)
normalvideo(COL, (i*INC)+ROW, MMenu[i]);
selectedMenu(COL,ROW, MMenu[0]);
i = done = 0;
do
{
/**Status Bar Code**/
setfillstyle(SOLID_FILL,BLUE);
settextstyle(SMALL_FONT,HORIZ_DIR,5);
bar(pixTOrc(2),pixTOrc(52.5),pixTOrc(75),pixTOrc(55));
setcolor(LIGHTCYAN);
switch(i)
{
case 0 : outtextxy(pixTOrc(5),pixTOrc(52.75),"View Account --> View Detail of an account");
break;
case 1 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Transactions --> Do transaction Debit/Credit");
break;
case 2 : outtextxy(pixTOrc(5),pixTOrc(52.75),"New Accont --> Create a new account for customer");
break;
case 3 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Edit Account --> Edit an existing account");
break;
case 4 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Close the Program");
break;
}
/**status Bar ends**/
setcolor(WHITE);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
int key = getch();
switch (key)
{
case 00: key = getch();
switch (key)
{
case 72: normalvideo(COL, (i*INC)+ROW, MMenu[i]);
i--;
if (i == -1)
i = MAXMENU-1;
selectedMenu(COL,(i*INC)+ROW,MMenu[i]);
break;
case 80: normalvideo(COL, (i*INC)+ROW, MMenu[i]);
i++;
if (i == MAXMENU)
i = 0;
selectedMenu(COL, (i*INC)+ROW, MMenu[i]);
break;
}
break;
case 13: done = 1;
}
}
while (!done);
return(i+49);
}
//This part you can use for main functionality of the menu
void mainArea()
{
setcolor(BLUE);
outtextxy(pixTOrc(30),pixTOrc(20),"http://cbsecsnip.in/");
setcolor(YELLOW);
}
/* Code for displaying the main menu*/
void call_menu()
{
char choice;
do
{
choice = menu();
switch (choice)
{
case '1': setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"View Account");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '2':
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Transactions");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '3':
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"New Accont");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '4':
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Edit Account");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '5': //Close the project
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Quit");
mainArea();
delay(1000);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
goto exit;
}
} while (choice != MAXMENU);
exit:
}
void main()
{
int i,j;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setcolor(BLACK);
rectangle(0,0,640,480);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(2,1,637,478);
setfillstyle(SOLID_FILL,BLACK);
bar(1,3,637,50);
settextstyle(BOLD_FONT,HORIZ_DIR,2);
setcolor(YELLOW);
outtextxy(pixTOrc(12),pixTOrc(1)," BANKING SYSTEM PROJECT ");
setfillstyle(CLOSE_DOT_FILL,DARKGRAY);
bar(pixTOrc(7),pixTOrc(14),pixTOrc(25),pixTOrc(50));
call_menu();
closegraph();
getch();
}
Possible Solutions:
Copy all Files from BGI folder to BIN.
Check if it works.