0

I am writting a simple program in C which gives the user an option what type of code to execute, however I am getting an error message (Debug assertion failed!). I am also trying to use cyrillic but also getting an error.

I have tried using else if() instead of switch,but with no result,and i don't know where to look next. For cyrillic i tried ConsoleOutputCP(1251) as well as ConsoleCP(1251) but neither of them worked.

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>

int choice(void);
int first (void);
int second(void);
int third(void);
int fourth(void);

int i;
int main() {

  do {
  choice();
  printf("%d", i);

    switch (i) {
    case 1: first();
        break;
    case 2: second();
        break;
    case 3: third();
        break;
    case 4: fourth();
    }
  } while (i != 5);

  system("pause");
  return 0;
}

int choice(void) {
  printf("izberete komanda\n");
  printf("1.chetene na programa ot fail i zapis na rezultata vuv fail\n");
  printf("2.chetene na programa ot fail i izvejdane na rezultata\n");
  printf("3.chetene na progarma ot klavietura i izvejdane vuv fail\n");
  printf("4.chetene na programa ot klavietura i izvejdane na ekrana\n ");
  printf("5.izhod\n");

  do {
    wscanf_s("%d", i);
    //i != getchar();
  } while (i < 1 || i>5);

  return i;
}
Simeon Ivanov
  • 37
  • 1
  • 4
  • Welcome to SO! What was the assertion that failed? Also, it is a good idea to learn to use a debugger -- it can show you things like the value of i when you get the assert. – Dave S Jan 03 '19 at 22:25
  • Remove `i != getchar();` from `choice`. It is unneeded/wrong. The value of `i` comes from `scanf_s`. Beyond that, what problems are you having with cyrillic? You're using windows, so you may need `wscanf_s` to do utf-16 but utf-8 is usually easier – Craig Estey Jan 03 '19 at 22:26
  • Also, choice() should be inside your loop (before the switch) not outside of it. – Dave S Jan 03 '19 at 22:29
  • So I have removed i!=getchar(),used wscanf_s and put choice() inside the loop.Now I stopped getting the error message,but the program stops right after I enter a number.About the cyrillic it looks like these two doesn't work for me because when i try to run the program it tells me that there are unicode characters that could not be saved in the current codepage. – Simeon Ivanov Jan 03 '19 at 22:49
  • You want to pass `&i` (address of `i`) as the parameter to `wscanf_s `. Without the ampersand, scanf will try to write its result to memory you don't want it to write to. Instead, you want to tell it to put the result at "the address of i" so that it shows up in `i` as you expect – Ben Zotto Jan 03 '19 at 22:53
  • Wow,I can't believe so many people answered in such short time.The program now runs but still can't use cyrillic.I have tried also 'setlocale(LC_ALL, "Bulgarian");'but still no result. – Simeon Ivanov Jan 03 '19 at 23:06
  • Only one person answered; you have received a number of comments. You should generally respond to comments by improving/clarifying the question (by editing it) rather then adding relevant information in further comments. SO is not a discussion forum. Your question about Cyrillic is best posted as a completely separate question; it is a clearly separate issue. It is platform specific too, so you will need to mention what you are running on. That said there is almost certainly an existing answer such as https://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app – Clifford Jan 03 '19 at 23:49

1 Answers1

0

First of all: you need to put choice() function inside your do while loop.
Second thing: There's no need for i != getchar();. And you can post your problem with cyrillic as second question. That way you could get answers faster.

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>

int choice(void);
int first (void);
int second(void);
int third(void);
int fourth(void);

int i;

int main(void) {
    do
    {
        i = choice();
        switch (i)
        {
        case 1:first();
            break;
        case 2:second();
            break;
        case 3:third();
            break;
        case 4:fourth();
        }
    } while (i != 5);


    system("pause");
    return 0;
}

int choice(void) {
    printf("izberete komanda\n");
    printf("1.chetene na programa ot fail i zapis na rezultata vuv fail\n");
    printf("2.chetene na programa ot fail i izvejdane na rezultata\n");
    printf("3.chetene na progarma ot klavietura i izvejdane vuv fail\n");
    printf("4.chetene na programa ot klavietura i izvejdane na ekrana\n ");
    printf("5.izhod\n");

    scanf("%d", &i);
    return i;
}
Physx
  • 128
  • 9