pomijanie scanf

0
// Sqlite.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sqlite3.h"
#include <Windows.h>

#pragma warning(disable:4996)

void display_table(sqlite3 *db, char *error, int rc);
void adding_student(sqlite3 *db, char *error, int rc);
void delete_student(sqlite3 *db, char *error, int rc);
void finding_student(sqlite3 *db, char *error, int rc);

int _tmain(int argc, _TCHAR* argv[])
{
	int rc;
	char *error;
	
	// Open Database
	sqlite3 *db;
	rc = sqlite3_open("MyDb.db", &db);
	if (rc)
	{
		printf("Error opening SQLite3 database: %s", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}
	else
	{
		printf("Opened MyDb.db.");
	}

	// Execute SQL
	const char *sqlCreateTable = "CREATE TABLE IF NOT EXISTS Studenci (id INTEGER PRIMARY KEY, imie STRING, nazwisko STRING, pesel STRING);";
	rc = sqlite3_exec(db, sqlCreateTable, NULL, NULL, &error);
	if (rc)
	{
		printf("Error creating SQLite3 database: %s", sqlite3_errmsg(db));
		sqlite3_free(error);
	}
	else
	{
		printf("Created If not exists MyTable.\n");
	}

	char opcja_menu;
	
	do
	{
		printf("\n============================================"
			"\nMenu:\n\n"
			"1. Wyswietl baze danych studentow.\n"
			"2. Dodaj nowego studenta.\n"
			"3. Usun studenta wedlug PESEL.\n"
			"4. Znajdz studenta wedlug PESEL.\n"
			"5. Wyswietl liczbe studentow\n"
			"q - Exit.\n\n");
		printf("Wybierz opcje: ");

		scanf("%c", &opcja_menu);
		//system("cls");
				
		switch (opcja_menu)
		{
//case 1 #################################
		case '1':
			system("cls");
			display_table(db,error,rc);
			break;
//case 2 ##################################
		case '2':
			system("cls");
			adding_student(db, error, rc);
			display_table(db, error, rc);
			break;
//case 3 ##################################
		case '3':
			system("cls");
			delete_student(db, error, rc);
			display_table(db, error, rc);
			break;
//case 4 ###################################
		case '4':
			system("cls");
			finding_student(db, error,rc);
			break;
//case 5 ###################################
		case '5':

			break;

		}

	} while (opcja_menu != 'q');

	// Close Database
	printf("Closing MyDb.db ...\n\n");
	sqlite3_close(db);
	printf("Closed MyDb.db");

	system("pause");

	return 0;
}
//#####################################
void display_table(sqlite3 *db, char *error, int rc)
{
	// Display MyTable
	printf("Retrieving values in MyTable ...\n");
	const char *sqlSelect = "SELECT * FROM Studenci;";
	char **results = NULL;
	int rows, columns;
	sqlite3_get_table(db, sqlSelect, &results, &rows, &columns, &error);
	if (rc)
	{
		printf("Error executing SQLite3 statement: %s \n", sqlite3_errmsg(db));
		sqlite3_free(error);
	}
	else
	{
		// Display Table
		for (int rowCtr = 0; rowCtr <= rows; ++rowCtr)
		{
			for (int colCtr = 0; colCtr < columns; ++colCtr)
			{
				// Determine Cell Position
				int cellPosition = (rowCtr * columns) + colCtr;

				printf(results[cellPosition]);
				printf(" ");
			}

			// End Line
			printf("\n");

		}
	}
	sqlite3_free_table(results);
}
//#######################
void adding_student(sqlite3 *db, char *error, int rc)
{
	char imie[255];
	char nazwisko[255];
	int pesel;
	char str[255];
	printf("Inserting a value into MyTable ...\n");

	printf("\nPodaj imie: ");
	scanf("%s", &imie);
	printf("\nPodaj nazwisko: ");
	scanf("%s", &nazwisko);
	printf("\nPodaj pesel: ");
	scanf("%d", &pesel);

	sprintf(str, "INSERT INTO Studenci VALUES (NULL, '%s', '%s', '%d')", imie, nazwisko, pesel);
	const char *sqlInsert = str;
	rc = sqlite3_exec(db, sqlInsert, NULL, NULL, &error);
	if (rc)
	{
		printf("Error executing SQLite3 statement: %s \n", sqlite3_errmsg(db));
		sqlite3_free(error);
	}
	else
	{
		printf("Inserted a value into MyTable.\n\n");
	}
}
//######################
void delete_student(sqlite3 *db, char *error, int rc)
{
	char str[255];
	int pesel_delete;

	printf("\n\nPodaj PESEL uzytkownika ktorego chcesz usunac:\n");
	scanf("%d", &pesel_delete);
	sprintf(str, "DELETE FROM Studenci WHERE pesel = %d", pesel_delete);
	const char *sqlInsert = str;
	rc = sqlite3_exec(db, sqlInsert, NULL, NULL, &error);
}
//##############################
void finding_student(sqlite3 *db, char *error, int rc)
{
	int find_student;
	char str[255];
	printf("\n\nPodaj PESEL szukanego studenta:\n");
	scanf("%d", &find_student);

	printf("Finding student in Studenci ...\n\n");


	sprintf(str, "SELECT * FROM Studenci WHERE pesel=%d;", find_student);
	const char *sqlFind_st = str;
	char **results = NULL;
	int rows, columns;
	sqlite3_get_table(db, sqlFind_st, &results, &rows, &columns, &error);

	if (rc)
	{
		printf("Error executing SQLite3 statement: %s \n", sqlite3_errmsg(db));
		sqlite3_free(error);
	}
	else
	{
		// Display Table
		for (int rowCtr = 0; rowCtr <= rows; ++rowCtr)
		{
			for (int colCtr = 0; colCtr < columns; ++colCtr)
			{
				// finding by pesel
				int cellPosition = (rowCtr * columns) + colCtr;
				printf(results[cellPosition]);
				printf(" ");
			}

			// End Line
			printf("\n");

		}
	}
	sqlite3_free_table(results);
}

Po wyborze którejś z opcji menu (oprócz 'q') za drugim obiegiem pętli do{}while() scanf zostaje pominięty co skutkuje wyświetleniem się menu 2 razy.. Dlaczego tak się dzieje?

pozdrawiam

2

Zapewne scanf wczytuje enter naciśnięty po wpisaniu wcześniejszego znaku. Przed konkretnym miejscem zamiast np. "%d" napisz " %d" czyli ze spacją wcześniej.

0

Super. Pomogło. Tylko dlaczego wczytuje ten enter? Taka już jest 'zaleta' scanf-a?

1

"%c" wczytuje ci jeden znak, a po naciśnięciu enter w strumieniu masz dwa znaki: to co wybrałeś i znak końca linii.
dodanie spacji informuje scanf by zignorować wszystkie białe znaki (spacje, tabulatory i koniec linii) i wskaźnik odczytu przesunie się do pierwszego nie białego znaku.

Pytanie, czy dokładnie ci o to chodzi:
Przykładowo w obecnym kodzie możesz też wpisać "12<enter>" i od razu po wczytaniu studentów twój kod będzie chciał dodać nowego studenta.
Dodaj przed scanf: fflush(stdin); które powinno opróżnić bufor danych bez względu na zawartość (działa w przypadku konsoli).

1 użytkowników online, w tym zalogowanych: 0, gości: 1