Scanf problem konwertowaniem typów i sprawdzanie danych typu integer

W tym miejscu zadajemy pytania na temat języka C++, dzielimy się swoją wiedzą, udzielamy wsparcia, rozwiązujemy problemy programistyczne.
Awatar użytkownika
foreste
User
User
Posty: 113
Rejestracja: niedziela 06 wrz 2015, 19:53
Kontaktowanie:

Scanf problem konwertowaniem typów i sprawdzanie danych typu integer

Postautor: foreste » wtorek 01 sie 2017, 14:39

Tak jak temacie mam problem z konwertowaniem danych z funkcji scanf do postaci integer i ich sprawdzaniem , kod splikacji

Kod: Zaznacz cały

#include "mbed.h"
#include "MPL3115A2.h"
#include "Terminal.h"
#include "eeprom.h"
#include "cstdlib"
#include "string"

#define EEPROM_ADDR 0x00   // I2c EEPROM address is 0x00

#define SDA PTE25            // I2C SDA pin
#define SCL PTE24           // I2C SCL pin
int32_t eeprom_size ,  addr;
Terminal pc(PTA2, PTA1); // tx, rx
I2C i2c(PTE0, PTE1);       // sda, scl
EEPROM brain(SDA, SCL, EEPROM_ADDR, EEPROM::T24C08);
MPL3115A2 sensor(&i2c, &pc);
int temperature_live_state, read_user_temp;
int val = 0;
Timer licznik;
char buffer[20];
char dane[4];

DigitalOut buzzer(PTA12);     // Sanity check to make sure the program is working.
DigitalOut myled1(PTD1, 1);     // Sanity check to make sure the program is working.

void serials()
{
   if (pc.readable())
   {
      pc.scanf("%s %s", buffer, dane);
   }
}

void alarm(string  & temp_read_input)
{
   string str = temp_read_input;
   temperature_live_state = atoi(str.c_str());
   if (temperature_live_state >= read_user_temp)
   {
      licznik.start();
   
      if (licznik.read_ms() <= 1000)
      {
         buzzer = !buzzer;
         //myled1 = !myled1;
      }
      licznik.reset();
   }
   if (read_user_temp > temperature_live_state)
   {
      buzzer = 0;
   }
   
}



void screen()
{
   pc.foreground(0xFFFF00);
   pc.hideCursor();
}
void intro()
{
   pc.locate(25, 5);
   pc.printf(" ** inteligentny Termometr   ** ");
   pc.locate(25, 6);
   pc.printf("Created by Foreste");
}
void labels()
{
   pc.locate(20, 7);
   pc.printf("Temperatura");
   pc.locate(35, 7);
   pc.printf("Cisnienie");
   pc.locate(48, 7);
   pc.printf("alarm wysokiej temperatury");
}
void input_add_user_define_temperature()
{
   fflush(stdin);
   screen();
   intro();
   pc.locate(1, 7);
   pc.printf("Wpisz wartość temperatury ,po której termometr bedzie sygnalizował przekroczenie");
   int tempint;

      
   
   
      
   while ((read_user_temp >= 1) && (read_user_temp <= 50))
   {
      
       
            tempint = atoi(dane); //kowertowanie tablicy typu char na zmienną int
            read_user_temp = tempint;
      
      
      
   }
                     
         //read_user_temp;
      if ((read_user_temp >= 12) && (read_user_temp <= 40))
      {
         pc.cls();
         screen();
         intro();
         brain.write(eeprom_size = 8, (int32_t) read_user_temp);
         pc.locate(20, 7);
         pc.printf("Zapisalem podaną temperature przekroczenia");
         wait(5);
         val = 1;
         fflush(stdin);
         pc.cls();
      }
      else
      {
         pc.cls();
         screen();
         //intro();
         pc.locate(20, 7);
         pc.printf("Wpisano niepoprawne dane. Dopuszczalny zakres: od 12 do 40");
         wait(5);
      }
   
}
   
void rs232_menu()
{
   
   if (strcmp(buffer, "reset") == 0)
   {
      NVIC_SystemReset();
   }
   else if (strcmp(buffer, "alarm") == 0)
   {
      pc.cls();
      input_add_user_define_temperature();
   }
   
}

int main() {
   
   pc.baud(9600);
   pc.attach(&serials);
   pc.cls();
   sensor.init();
   screen();
   intro();
   wait(5);
   //Altitude a;
   Temperature t;
   Pressure p;                                                                                                                                     
   // Offsets for Dacula, GA
   //sensor.setOffsetAltitude(83);
   sensor.setOffsetTemperature(20);
   sensor.setOffsetPressure(-32);
   brain.read((uint32_t)(eeprom_size = 8), (int32_t&) read_user_temp);
   
   while (read_user_temp <= 0)
   {
      while (val <= 0)
      {
         input_add_user_define_temperature();
      }
   }
   pc.cls();
   
   
   while (1)
   {
      sensor.readTemperature(&t);
        sensor.setModeStandby();
      sensor.setModeBarometer();
      sensor.setModeActive();
      sensor.readPressure(&p);
      
      screen();
      intro();
      labels();
      string temp_cache = t.print();
      pc.locate(20, 9);
      pc.printf("%s C", t.print());
      pc.locate(35, 9);
      pc.printf("%s Hpa", p.print());
      pc.locate(50, 9);
      pc.printf("%d", read_user_temp);
      alarm(temp_cache);
      rs232_menu();
      
      
      //pc.printf("%s", test);
      sensor.setModeStandby();
      sensor.setModeAltimeter();
      sensor.setModeActive();
      
   }
}

a kod z problemem to

Kod: Zaznacz cały

void input_add_user_define_temperature()
{
   fflush(stdin);
   screen();
   intro();
   pc.locate(1, 7);
   pc.printf("Wpisz wartość temperatury ,po której termometr bedzie sygnalizował przekroczenie");
   int tempint;

      
   
   
      
   while ((read_user_temp >= 1) && (read_user_temp <= 50))
   {
      
       
            tempint = atoi(dane); //kowertowanie tablicy typu char na zmienną int
            read_user_temp = tempint;
      
      
      
   }
                     
         //read_user_temp;
      if ((read_user_temp >= 12) && (read_user_temp <= 40))
      {
         pc.cls();
         screen();
         intro();
         brain.write(eeprom_size = 8, (int32_t) read_user_temp);
         pc.locate(20, 7);
         pc.printf("Zapisalem podaną temperature przekroczenia");
         wait(5);
         val = 1;
         fflush(stdin);
         pc.cls();
      }
      else
      {
         pc.cls();
         screen();
         //intro();
         pc.locate(20, 7);
         pc.printf("Wpisano niepoprawne dane. Dopuszczalny zakres: od 12 do 40");
         wait(5);
      }
   
}

linijka

Kod: Zaznacz cały

while ((read_user_temp >= 1) && (read_user_temp <= 50))
robi za symulowany blokadę wykonywania kodu do w czasu wpisania danych
sama funkcja gets i scanf blokują kod, ale ja dałem scanf n a przerwaniach by nie blokował pętli while(1) pod koniec w tym miejscu akurat , .=potrzebuje blokady do czasu wysłania danych, tym też mam problem.
Dane scanf są pobierane przez

Kod: Zaznacz cały

void serials()
{
   if (pc.readable())
   {
      pc.scanf("%s %s", buffer, dane);
   }
}

w sekcji main jest aktywowany kodem

Kod: Zaznacz cały

pc.attach(&serials);
♫,♫.¨♫

Wróć do „Pisanie programów w C++”

Kto jest online

Użytkownicy przeglądający to forum: Obecnie na forum nie ma żadnego zarejestrowanego użytkownika i 2 gości