Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. problem

problem

Scheduled Pinned Locked Moved ATL / WTL / STL
helptutorial
4 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samosato
    wrote on last edited by
    #1

    hi all I got following problem : I want to read and variable (age ) but when its not properly entered I want to write bad format or etc. example printf("enter age \n"); scanf ("%d,&age"); and I need to create a condition which is saying in case you enter string or non integer number (7,5 ) write bad format eter again thanks

    N L A 3 Replies Last reply
    0
    • S samosato

      hi all I got following problem : I want to read and variable (age ) but when its not properly entered I want to write bad format or etc. example printf("enter age \n"); scanf ("%d,&age"); and I need to create a condition which is saying in case you enter string or non integer number (7,5 ) write bad format eter again thanks

      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #2

      This is a common problem of scanf(). You can use this[^] technique to solve your problem. NOTE: Next time when you want to post a C question please use the C\C++\MFC forum[^].

      1 Reply Last reply
      0
      • S samosato

        hi all I got following problem : I want to read and variable (age ) but when its not properly entered I want to write bad format or etc. example printf("enter age \n"); scanf ("%d,&age"); and I need to create a condition which is saying in case you enter string or non integer number (7,5 ) write bad format eter again thanks

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Use the features of the function to verify your input; see the MSDN documentation here[^]. A simple loop checking that you have a converted field and that its value is within the range you require should do the trick.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        1 Reply Last reply
        0
        • S samosato

          hi all I got following problem : I want to read and variable (age ) but when its not properly entered I want to write bad format or etc. example printf("enter age \n"); scanf ("%d,&age"); and I need to create a condition which is saying in case you enter string or non integer number (7,5 ) write bad format eter again thanks

          A Offline
          A Offline
          Alain Rist
          wrote on last edited by
          #4

          Hi, Supposing you search in this forum a C++ STL solution :)

          #include<iostream>
          #include<strstream>
          #include<string>

          int main()
          {
          using std::cout;
          using std::endl;

          // This is what you want
          cout << "Enter age" << endl;
          int age = -1;
          std::string s;
          while (age < 0)
          {
          	std::getline(std::cin, s);
          
          	if (s.empty())
          		cout << "Try again!" << endl;
          	else if (s.find\_first\_not\_of("0123456789") != s.npos)
          		cout << s << " is not a valid age. Try again!" << endl;
          	else
          		std::istrstream(s.c\_str()) >> age;
          
          	const int HighestAgeEver = 122;
          	if (age > HighestAgeEver)
          	{
          		cout << age << "!! You are kidding. Try again!" << endl;
          		age = -1;
          	}
          }
          
          cout << "Your age is " << age << endl;
          
          return 0;
          

          }

          cheers, AR Edited to handle empty input and unrealistic age :) And for simpler code flow

          When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

          modified on Wednesday, October 13, 2010 1:37 PM

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups