i'm trying to increment and decrement values on an ncurses window using mouse clicks, everything is working fine, but if i decrement up till the point where the number is negative then try to increment again (like -1), the second character (the 1) will stay showing on the WINDOW and the main character in the position where i change the number (-) will change, i realise that the problem is that i am changing only in the position event.x event.y with mvwprintw
so the question is, what is the most simple way (an ncurses function maybe) that allows me to delete that.
while((ch = getch()) != KEY_F(2)) {
switch(ch) {
case KEY_MOUSE :
if (getmouse(&event) == OK) {
if (event.y == 39 && event.x >= 1 && event.x <= 10) {
item_actif = PLUS;
mvwprintw(fen_outils, 0, 1, "X");
mvwprintw(fen_outils, 1, 1, " ");
wrefresh(fen_outils);
}
else if (event.y == 40 && event.x >=1 && event.x <= 10) {
item_actif = MINUS;
mvwprintw(fen_outils, 0, 1, " ");
mvwprintw(fen_outils, 1, 1, "X");
wrefresh(fen_outils);
}
else if (event.y > 0 && event.y < NB_LIGNES_SIM + 1 && event.x > 0 && event.x < NB_COL_SIM + 1) {
switch (item_actif) {
case PLUS :
k=0;
l=0;
for(i = 0;i<36;i+=4){
k++;
l=0;
for(j=19;j<129;j+=11){
if((event.y==i+1 && event.x==j+1)){
++tab[k][l];
sprintf(tabChar[k][l],"%d",tab[k][l]);
mvwprintw(fen_sim, event.y - 1, event.x - 1, tabChar[k][l]);
wrefresh(fen_sim);
refresh();
break;
}
l++;
}
}
break;
case MINUS :
k=0;
l=0;
for(i = 0;i<36;i+=4){
k++;
l=0;
for(j=19;j<129;j+=11){
if((event.y==i+1 && event.x==j+1)){
--tab[k][l];
sprintf(tabChar[k][l],"%d",tab[k][l]);
mvwprintw(fen_sim, event.y - 1, event.x - 1, tabChar[k][l]);
wrefresh(fen_sim);
refresh();
break;
}
l++;
}
}
}
}
}
}
}
Control PANEL ncurses
i found the simplest (but not the best) answer which is to add in both cases an :
if(tab[k][l] < 0){
mvwprintw(fen_sim, event.y -1, event.x -1, " ");
wrefresh(fen_sim);
}
Related
I'm getting the wrong output. The black screen is my output and the white screen is the right output. In my output the "V" is always in the right placement but the symbols under that line is always in the wrong placement.
: (hyphen) represents undiscovered locations
! : represents bombs
$ : represents treasure
& : represents both a bomb and treasure
. : represents a visited location that had neither a bomb nor a treasure
Check for BOTH a bomb AND a treasure
Check both the bomb and treasure member arrays to see if a value of 1 is set for both at the same
location
Update the player’s counters accordingly (bomb: reduce lives, treasure: increase treasure counter)
Display an appropriate message (use symbol: [&] to denote a bomb AND treasure, the treasure is
considered a “life insurance payout”)
If there is no bomb or treasure at the location entered by the user
Display an appropriate message (use symbol: [.] to denote nothing found)
This is my code:
do
{
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else
{
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n");
do
{
lives = 0;
printf("Next Move [1-%d]: ", gameSettings.gameLength);
scanf("%d", &playerData.currentMove);
if (playerData.currentMove >= 1 && playerData.currentMove <= gameSettings.gameLength)
{
printf("\n");
lives++;
}
else
{
printf(" Out of Range!!!\n");
}
} while (lives < 1);
if (gameSettings.revealedPosition[playerData.currentMove] == hiddenPosition)
{
if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [.] ...Nothing found here... [.]\n\n");
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [!] !!! BOOOOOM !!! [!]\n\n");
playerData.playerLives -= 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
printf("===============> [$] $$$ Found Treasure! $$$ [$]\n\n");
playerData.treasureNum += 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
printf("===============> [&] !!! BOOOOOM !!! [&]\n");
printf("===============> [&] $$$ Life Insurance Payout!!! [&]\n\n");
gameSettings.gameMoves -= 1;
playerData.treasureNum += 1;
playerData.playerLives -= 1;
}
}
else
{
printf("===============> Dope! You've been here before!\n\n");
}
} while (playerData.playerLives != 0 && gameSettings.gameMoves != 0);
printf("No more LIVES remaining!\n\n");
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else {
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n\n");
printf("##################\n# Game over! #\n##################\n\n");
printf("You should play again and try to beat your score!!\n");
return 0;
Here is the .c code from start to finish. I suspect it's an order of operations issue or something else.
The code is supposed to have a title screen where you can exit or play. If you hit play it renders a new char array in which you can move either left, right, or down (a/d/s). You get points for going down a row each time as well as going over 'P' spots on the array.
Once you hit y-coordinate 202 on the array as well as have positive points, you win and it renders a congrats char array. If you go negative in points (by hitting the '#' paces or the 'L' wall that goes down a row every 1/2 second) then you will lose and be told you lost in a new char array as well as be asked if you want to exit or play again.
And as mentioned before, the movement for the 'L's and the play character 'O' simply doesn't move.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
char quickchar(float delay);
void render();
void titleRender();
void renderLoss();
void renderWin();
int score();
char titlescreen[20][59] = { };
char diffdiff[20][46] = {
(Grid removed due to char limit)
};
char gameover[20][50] = {
(Grid removed due to char limit)
};
char gameboard[203][26] = {
(Grid removed due to char limit)
};
int playerx;
int playery;
int pts;
void entryRender();
void gameRender();
int main() {
int input;
char replay;
int lx;
int ly;
char walkin;
int gaming;
char titlechoice;
int diff;
int running;
lx = 0;
ly = 0;
running = 1;
playerx = 13;
playery = 5;
pts = 25;
gaming = 0;
while (gaming == 0) {
titleRender();
printf("\033[1;32m");
printf("\nPress 'p' to play or 'x' to exit!");
printf("\033[0m");
scanf(" %c", &titlechoice);
if (titlechoice == 'p') {
gaming += 4;
}
if (titlechoice == 'x') {
return 0;
}
}
while (gaming == 4) {
system("stty -echo raw");
render();
printf("\033[1;35m");
printf("Choose a direction: s/Down, a/Left, d/Right.\n\r");
printf("\033[0m");
walkin = quickchar(0.5);
scanf("%c", &walkin);
//obj behaviour
if (walkin == 's' &&
gameboard[playery +1][playerx] != '#' &&
gameboard[playery + 1][playerx] != '+' &&
gameboard[playery + 1][playerx] != '|' &&
gameboard[playery + 1][playerx] != '-') {
playery++;
pts += 5;
if (gameboard[playery + 1][playerx] == '#') {
pts -= 25;
}
if (gameboard[playery + 1][playerx] == 'P') {
printf("Points increased!\n\r");
pts += 50;
gameboard[playery + 1][playerx] = '_';
}
}
else if (walkin == 'a' &&
gameboard[playery][playerx - 1] != '#' &&
gameboard[playery][playerx - 1] != '+' &&
gameboard[playery][playerx - 1] != '|' &&
gameboard[playery][playerx - 1] != '-') {
playerx--;
if (gameboard[playery][playerx - 1] == '#') {
pts -= 25;
}
if (gameboard[playery][playerx - 1] == 'P') {
printf("Points increased!\n\r");
pts += 50;
gameboard[playery][playerx - 1] = '_';
}
}
else if (walkin == 'd' &&
gameboard[playery][playerx + 1] != '#' &&
gameboard[playery][playerx + 1] != '+' &&
gameboard[playery][playerx + 1] != '|' &&
gameboard[playery][playerx + 1] != '-') {
playerx++;
if (gameboard[playery][playerx + 1] == '#') {
pts -= 25;
}
if (gameboard[playery + 1][playerx] == 'P) {
printf("Points increased!\n\r");
pts += 50;
gameboard[playery][playerx + 1] = '_';
}
}
if (walkin == 'a' || walkin == 's' || walkin == 'd' ||
walkin != 'a' || walkin != ' ' || walkin != 'd' ) {
for (ly = 0; ly = 202; ly++) {
for (lx = 1; lx = 25; lx++) {
gameboard[ly][lx] = 'L';
}
}
}
if (pts <= 0) {
system("clear");
renderLoss();
scanf("%c", replay);
if (replay == 'Y') {
gaming = 3;
}
if (replay == 'N') {
gaming = 5;
}
}
if (playery == 202) {
renderWin();
printf("");
printf("Congrats! You got a score of %d !", pts);
printf("");
scanf("%c", &replay);
if (replay == 'Y') {
gaming = 4;
}
if (replay == 'N') {
gaming = 5;
}
}
//player postion == 203y, then congrta screen
//ask player to set gaming to 4(replay) or none (exit program)
}
system("stty echo -raw");
return 0;
}
void render() {
int y,x,k;
system("clear");
printf("\033[01;33m");
printf("||POINTS: %d||\r\n", pts);
printf("\033[0m");
for (y = 0; y < 203; y++) {
for (x = 0; x < 26; x++) {
if (y == playery && x == playerx) {
printf("\033[0;32m");
printf("O");
printf("\033[0m");
}
else {
printf("\033[1;36m");
printf("%c", gameboard[y][x]);
printf("\033[0m");
}
}
printf("\r\n");
}
}
void titleRender() {
int y, x;
system("clear");
for (y = 0; y < 20; y++) {
for (x = 0; x < 59; x++) {
printf("\033[0;32m");
printf(" %c", titlescreen[y][x]);
printf("\033[0m");
}
printf("\r\n");
}
}
void renderLoss() {
int y, x;
system("clear");
for (y = 0; y < 26; y++) {
for (x = 0; x < 50; x++) {
printf("\033[0;31m");
printf(" %c", gameover[y][x]);
printf("\033[0m");
}
printf("\r\n");
}
}
void renderWin() {
int y, x;
system("clear");
for (y = 0; y < 20; y++) {
for (x = 0; x < 46; x++) {
printf("\033[0;33m");
printf(" %c", diffdiff);
printf("\033[0m");
}
printf("\r\n");
}
}
char quickchar(float delay) {
int flags;
char c;
usleep((int)(delay*1000000));
flags = fcntl(0, F_GETFL, 0);
fcntl(0, F_SETFL, flags | O_NONBLOCK);
c = getchar();
fcntl(0, F_SETFL, flags ^ O_NONBLOCK);
return c;
}
Following my previous question about making a snake program more fluid, I went and tried ANSI escape sequences and console functions to put back the text cursor on the top left corner.
I want to get the cursor on the top left corner of the screen, but while it does so I get black stripes across the screen every other line.
I am working in a windows environment making this program for the windows console.
Here is the painting function :
void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
//system("cls");
//printf("\033[1;1H");
COORD destCoord;
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hconsole, destCoord);
for (int i = 0; i < ligneMax; i++)
{
for (int j = 0; j < colonneMax; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
I tried putting the escape code and console function in the main right before calling the paint function but I got the same results.
here is the whole program if someone wants to test :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
//system("cls");
//printf("\033[1;1H");
COORD destCoord;
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hconsole, destCoord);
for (int i = 0; i < ligneMax; i++)
{
for (int j = 0; j < colonneMax; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(int tab[28][120], int Nbligne, int Nbcolonne,
int randligne, int randcols)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == Nbligne - 1)
tab[i][j] = 205;
if (j == 0 || j == Nbcolonne - 1)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == Nbcolonne - 1)
tab[i][j] = 187;
if (i == Nbligne - 1 && j == 0)
tab[i][j] = 200;
if (i == Nbligne - 1 && j == Nbcolonne - 1)
tab[i][j] = 188;
if (i == 14 && j == 60)
tab[i][j] = 219;
if (i == 14 && j == 59)
tab[i][j] = 79;
if (i == 14 && j == 58)
tab[i][j] = 35;
if (i == randligne && j == randcols)
tab[i][j] = 176;
}
}
}
void destroyTail(int tab[28][120], int Nbligne, int Nbcolonne)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if (tab[i][j] == 35)
{
tab[i][j] = ' ';
if (tab[i][j + 1] == 79)
tab[i][j + 1] = 35;
else if (tab[i][j - 1] == 79)
tab[i][j - 1] = 35;
else if (tab[i + 1][j] == 79)
tab[i + 1][j] = 35;
else if (tab[i - 1][j] == 79)
tab[i - 1][j] = 35;
goto stop;
}
}
}
stop: NULL;
}
void translate(int tab[28][120], char direction, int Nbligne, int Nbcolonne)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if (tab[i][j] == 219)
{
if (direction == 'R')
{
tab[i][j] = 79;
tab[i][j + 1] = 219;
}
if (direction == 'D')
{
tab[i][j] = 79;
tab[i + 1][j] = 219;
}
if (direction == 'L')
{
tab[i][j] = 79;
tab[i][j - 1] = 219;
}
if (direction == 'U')
{
tab[i][j] = 79;
tab[i - 1][j] = 219;
}
goto stop;
}
}
}
stop: NULL;
}
int checkExpand(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if ((direction == 'R' && tab[i][j] == 219 && tab[i][j + 1] == 176) ||
(direction == 'L' && tab[i][j] == 219 && tab[i][j - 1] == 176) ||
(direction == 'U' && tab[i][j] == 219 && tab[i - 1][j] == 176) ||
(direction == 'D' && tab[i][j] == 219 && tab[i + 1][j] == 176))
return 1;
}
}
return 0;
}
int checkDeath(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if ((direction == 'R' && tab[i][j] == 219 && (tab[i][j + 1] == 186 || tab[i][j + 1] == 79)) ||
(direction == 'L' && tab[i][j] == 219 && (tab[i][j - 1] == 186 || tab[i][j - 1] == 79)) ||
(direction == 'U' && tab[i][j] == 219 && (tab[i - 1][j] == 205 || tab[i - 1][j] == 79)) ||
(direction == 'D' && tab[i][j] == 219 && (tab[i + 1][j] == 205 || tab[i + 1][j] == 79)))
return 1;
}
}
return 0;
}
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 241);
int tab[28][120];
int randligne = rand() % 26 + 1;
int randcols = rand() % 118 + 1;
create(tab, 28, 120, randligne, randcols);
paint(tab, 28, 120, hConsole);
char i = '1';
char direction = 'R';
int eaten = 0;
int expand = 0;
int death = 0;
while(i != 'k')
{
if (kbhit())
i = getch();
switch(i) {
case 'z':
if (direction != 'D')
direction = 'U';
break;
case 's':
if (direction != 'U')
direction = 'D';
break;
case 'd':
if (direction != 'L')
direction = 'R';
break;
case 'q':
if (direction != 'R')
direction = 'L';
break;
}
randligne = rand() % 26 + 1;
randcols = rand() % 118 + 1;
death = checkDeath(tab, 28, 120, direction);
if (death == 1)
break;
translate(tab, direction, 28, 120);
expand = checkExpand(tab, 28, 120, direction);
if (expand == 0)
destroyTail(tab, 28, 120);
else
{
while (tab[randligne][randcols] != ' ')
{
randligne = rand() % 26 + 1;
randcols = rand() % 118 + 1;
}
tab[randligne][randcols] = 176;
eaten++;
}
printf("Number of biscuits eaten : %d ; direction : %c ; expand : %d", eaten, direction, expand);
paint(tab, 28, 120, hConsole);
Sleep(100);
}
while(i != 'k')
{
if (kbhit())
i = getch();
}
}
Too much code to take in quickly. If you have Windows console ouput for the snake, can't you simply printf the next line and leave it at that?
If you want to hide the cursor (instead of trying to park it out of sight) you can make it invisible by calling SetConsoleCursorInfo and the struct passed is shown here.
I need help with my programming assignment, I have everything done except for one part! Here is the assignment:
Problem 1: String Operations and Data Security: To improve data security in the transmission of data and information, dynamic character coding is being practiced. The modification of the original characters can be using the first 8 prime members [1, 2, 3, 5, 7, 11, 13, 17]: First character enhanced by 1; second character by 2, third by 3, .. 8th character by 17. Next 8 characters use the prime numbers in the reverse order 17..1, and decrease the values. Use a total message of at least 64 characters [in quantities of 8 characters] and repeat the process of modifying 1-17 for the first 8; modifying by 17 -1 for next 8, and so on. Make your own message. After the message is coded, decoding should also be done, to restore the original message. You may want to change the lower case and upper case transitions as well.
Example: Original Message A B C D. …..
Normal ASCII 65 66 67 68 ….
Prime Numbers 1 2 3 5 ….
Enhanced ASCII 66 68 70 73 ….
Coded Message B D F I ……
I'm having trouble printing out the prime numbers without messing up the code afterwards such as the encoded and decoded ASCII codes and the encoded and decoded codes. This is my code so far:
If you could help me somehow that would be great! I would really appreciate it, thank you.
size_t x;
int i, c;
i = 1;
c = 0;
char text[65];
int s[8] = {1, 2, 3, 5, 7, 11, 13, 17};
printf("Enter a line of text: "); // prompts user to enter text
fgets(text, 65, stdin); // reads input from user
printf("\nOriginal Message: ");
for(x = 0; x < strlen(text); ++x) // for loop to print original message
{
printf("%c", text[x]);
} // end for
// ASCII Code
printf("\nASCII Code: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
printf("%d ", text[x]);
}
// prime numbers
// my issue is here
printf("\n\nPrime Numbers: ");
for(x = 0; x <= 8 ; ++x)
{
if(c == 0)
{
if(i == 1)
{
printf("1 ");
++i;
}
else if(i == 2)
{
printf("2 ");
++i;
}
else if(i == 3)
{
printf("3 ");
++i;
}
else if(i == 4)
{
printf("5 ");
++i;
}
else if(i == 5)
{
printf("7 ");
++i;
}
else if(i == 6)
{
printf("11 ");
++i;
}
else if(i == 7)
{
printf("13 ");
++i;
}
else if(i == 8)
{
printf("17 ");
++c;
++x;
}
}
if(c == 1)
{
if(i == 8)
{
printf("17 ");
--i;
}
else if(i == 7)
{
printf("13 ");
--i;
}
else if(i == 6)
{
printf("11 ");
--i;
}
else if(i == 5)
{
printf("7 ");
--i;
}
else if(i == 4)
{
printf("5 ");
--i;
}
else if(i == 3)
{
printf("3 ");
--i;
}
else if(i == 2)
{
printf("2 ");
--i;
}
else if(i == 1)
{
printf("1 ");
--c;
}
} // end outer if
} // issue ends here
for(x = 0; x < strlen(text) - 1; ++x)
{
if(c == 0) // outer if statement increasing
{
if(i == 1) // inner if statement
{
text[x] = text[x] + 1;
++i;
}
else if (i == 2)
{
text[x] = text[x] + 2;
++i;
}
else if (i == 3)
{
text[x] = text[x] + 3;
++i;
}
else if(i == 4)
{
text[x] = text[x] + 5;
++i;
}
else if(i == 5)
{
text[x] = text[x] + 7;
++i;
}
else if(i == 6)
{
text[x] = text[x] + 11;
++i;
}
else if(i == 7)
{
text[x] = text[x] + 13;
++i;
}
else if(i == 8)
{
text[x] = text[x] + 17;
++c;
++x;
} // end if statement
} // end outer if statement
if(c == 1) // outer if statement decreasing
{
if(i == 8)
{
text[x] = text[x] + 17;
--i;
}
else if(i == 7)
{
text[x] = text[x] + 13;
--i;
}
else if(i == 6)
{
text[x] = text[x] + 11;
--i;
}
else if(i == 5)
{
text[x] = text[x] + 7;
--i;
}
else if(i == 4)
{
text[x] = text[x] + 5;
--i;
}
else if(i == 3)
{
text[x] = text[x] + 3;
--i;
}
else if(i == 2)
{
text[x] = text[x] + 2;
--i;
}
else if(i == 1)
{
text[x] = text[x] + 1;
--c;
}
} // end outer if
} // end for
printf("\n\nEncrypted Message: ");
for(x = 0; x <= strlen(text) - 1; ++x)
{
printf("%c", text[x]);
}
printf("\nEncrypted ASCII: ");
for(x = 0; x < strlen(text) - 1; x++)
{
printf("%d ", text[x]);
}
c = 0;
i = 1;
for(x = 0; x < strlen(text) - 1; ++x)
{
if(c == 0)
{
if(i == 1)
{
text[x] = text[x] - 1;
++i;
}
else if(i == 2)
{
text[x] = text[x] - 2;
++i;
}
else if(i == 3)
{
text[x] = text[x] - 3;
++i;
}
else if(i == 4)
{
text[x] = text[x] - 5;
++i;
}
else if(i == 5)
{
text[x] = text[x] - 7;
++i;
}
else if(i == 6)
{
text[x] = text[x] - 11;
++i;
}
else if(i == 7)
{
text[x] = text[x] - 13;
++i;
}
else if(i == 8)
{
text[x] = text[x] - 17;
++c;
++x;
} // end if statement
} // end outer if statement
if(c == 1)
{
if(i == 8)
{
text[x] = text[x] - 17;
--i;
}
else if(i == 7)
{
text[x] = text[x] - 13;
--i;
}
else if(i == 6)
{
text[x] = text[x] - 11;
--i;
}
else if(i == 5)
{
text[x] = text[x] - 7;
--i;
}
else if(i == 4)
{
text[x] = text[x] - 5;
--i;
}
else if(i == 3)
{
text[x] = text[x] - 3;
--i;
}
else if(i == 2)
{
text[x] = text[x] - 2;
--i;
}
else if(i == 1)
{
text[x] = text[x] - 1;
--c;
} // end inner if statements
} // end outer if statements
} // end for
printf("\n\nDecrypted Message: ");
for(x = 0; x < strlen(text); ++x)
{
printf("%c", text[x]);
}
printf("\nDecrypted ASCII: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
printf("%d ", text[x]);
}
puts(" ");
} // end main
Characters are represented by numbers. For example, for ASCII, z is represented by the number 122. If you add something to that value then you get a new value (e.g. 122 + 17 = 139). That new value may not be valid ASCII (e.g. 128 or larger), and might be a control code (e.g. 127 is the "delete" control character).
Because the values may no longer represent valid characters, you can't print them as characters. You need to print them as values (e.g. for(x = 0; text[x] != 0; x++) { printf("%d ", text[x]); }).
Worse, for "signed char", the addition can cause overflow, which is undefined behaviour in C. You'd need to use a different data type (e.g. unsigned char or uint8_t) to ensure the code has defined/expected behaviour.
Also note that your long chain of "if .. else if ... else if" statements should be a "switch()" with cases; more like this:
switch(i) {
case 1:
text[x] = text[x] + 1;
++i;
break;
case 2:
text[x] = text[x] + 2;
++i;
break;
...
}
However; by using int s[16] = {1, 2, 3, 5, 7, 11, 13, 17, 17, 13, 11, 7, 5, 3, 2, 1}; you'd be able replace many lines of similar code with something like this:
for(x = 0; text[x] != 0; x++) {
text[x] += s[x % 16]);
}
Thank you guys for answering my first question, but I have another one. I have this code here
that uses strtol() function. It works, but the problem is that if I enter one of the three options: (d 2) its considered as a string, and if Input (2d) its a string, and (d2) is a string. Is there a way to detect the numbers within the array?
char userInput[256];
char *end;
printf("Please enter your name: ");
scanf("%s", userInput);
int userName = strtol(userInput, &end, 10);
if (!*end)
{
printf("You have enter an int");
}
else
{
printf("You have entered a string");
}
If you really just need to check to see if string has numbers in it, this will do:
int has_ints(char * str) {
const int len = strlen(str);
int i;
for (i = 0; i < len; i++) {
if (str[i] <= '9' && str[i] >= '0') {
return 1;
}
}
return 0;
}
I did this:) works quite beautifully!
int exit = 0;
int i;
while (exit != 1)
{
char userInput[256] = {0};
int asciiCode[256];
int zeroCounter = 0;
int letterProof = 0;
int numberProof = 0;
int specicalCharactersProof = 0;
printf("\nPlease enter your name: ");
fgets(userInput, 256, stdin);
for (i = 0; i < 256; i++)
{
asciiCode[i] = userInput[i];
if (asciiCode[i] == 0)
{
zeroCounter++;
}
}
int reSize = (256 - zeroCounter);
for (i = 0; i < reSize; i++)
{
if (asciiCode[i] >= '0' && asciiCode[i] <= '9')
{
numberProof = 1;
}
else if ((asciiCode[i] >= 'a' && asciiCode[i] <= 'z') || (asciiCode[i] >= 'A' && asciiCode[i] <= 'Z'))
{
letterProof = 1;
}
else if ((asciiCode[i] >= '!' && asciiCode[i] <= '/') || (asciiCode[i] >= ':' && asciiCode[i] <= '#') || (asciiCode[i] >= '[' && asciiCode[i] <= '`') || (asciiCode[i] >= '{' && asciiCode[i] <= '~'))
{
specicalCharactersProof = 3;
}
}
if (letterProof == 1 && numberProof == 0 && specicalCharactersProof == 0)
{
printf("\nWelcome %s\n", userInput);
exit = 1;
}
else
{
printf("\nInvalid Input.\n");
}
}
return(0);
}