GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2004, 04:08
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

Need Help Modifying Code


I need to modify this code so that if anything other than a digit is typed, the input is considered to be an error. I tried several codes, but I have not been successful. Can someone offer some help?

CPP / C++ / C Code:
do
{
	printf("Input a positive integer:  ");
	fgets(line, MAXLINE, stdin);
	error = sscanf(line, "%d", &n) != 1 || n <= 0 ;
	if (error)
		printf("\nERROR: Do it again.\n");
} while (error)
Last edited by dsmith : 08-Nov-2004 at 06:26. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 08-Nov-2004, 06:50
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi sosy2001. The code works fine for me, except in cases where a real value is entered. I added a strchr command to look for a decimal. I also added a semicolon after the while statement (necessary to compile) and put it into a small program. Works fine. The logic seems sound and it is pretty efficient.

CPP / C++ / C Code:
#include <stdio.h>
#define MAXLINE 100
int main()
{

int n, error;
char line[MAXLINE];

	do{
		printf("Input a positive integer:  ");
		fgets(line, MAXLINE, stdin);
		error = sscanf(line, "%d", &n) != 1 || n <= 0 || strchr(line,'.');
		if (error)
			printf("\nERROR: Do it again.\n");
	} while (error);		//Semicolon missing....
}

Cheers.
  #3  
Old 08-Nov-2004, 08:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by sosy2001
I need to modify this code so that if anything other than a digit is typed, the input is considered to be an error.

Can you be a little more specific?

For example, what if the user enters 123a

Is this OK (with the integer value = 123, and everything else discarded)?

Regards,

Dave
  #4  
Old 08-Nov-2004, 18:41
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi sosy2001. I am sorry! I didn't fully test that! I was so enamored with the logic that I overlooked full testing!!!!

Unfortunately, I couldn't figure out how to make it work the way you had it. There may be a much better solution than this, but this is what I came up with:

CPP / C++ / C Code:
#include <stdio.h>
#define MAXLINE 100


int main()
{

int n, error;
char line[MAXLINE];

	do{
		printf("Input a positive integer:  ");
		fgets(line, MAXLINE, stdin);
		error = 0;
		for(n=0;line[n] && !error;n++)
			error = (isdigit(line[n]) == 0) && (line[n] != 10);
		if (error)
			printf("\nERROR: Do it again.\n");
	} while (error);		//Semicolon missing....
}

Again, I am sorry about that. It is easy to see how important thorough testing is
  #5  
Old 09-Nov-2004, 14:11
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

Need More Help


This code did not work either. The main focus of this code is sscanf, and to print only integers or display the error message.

Sorry for being so complicated.And thanks for your assistance.
  #6  
Old 10-Nov-2004, 06:59
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi sosy2001. Just curious, did the code not work or is it not acceptable because it does not use sscanf? I am certainly not above bad programs (I write them everyday ), but that code compiled and worked for every input that I threw at it.

If it is that sscanf is required, I will take a look at that and see if I can think of a way to do that...
  #7  
Old 10-Nov-2004, 17:26
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

I got errors


I tried to use it but i got some error messages. I will foward these error messages to you in a few minutes.

Thanks.

sosy2001
  #8  
Old 10-Nov-2004, 18:16
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

Here Is the Error


First the assignment question:

Examine the typical use of sscanf().Write a program that prompts the user to input a positive integer. Guard against errors so that a string typed by the user is picked up.

CPP / C++ / C Code:
char line[MAXLINE];
	int error, n;
  do{
    printf("Input a positive integer:  ");
    fgets(line, MAXLINE, stdin);
              error = sscanf(line, "%d",&n) !=1||n<=0;
    if (error)

      printf("\nERROR: Do it again.\n");
  } while (error);

athus doesnot catch all the errors. For example 12X in typed instead of 123, the error is not caught. So I have to modify this code to fix this problem.
  #9  
Old 10-Nov-2004, 21:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by sosy2001
First the assignment question:

Examine the typical use of sscanf().Write a program that prompts the user to input a positive integer. Guard against errors so that a string typed by the user is picked up.

CPP / C++ / C Code:
char line[MAXLINE];
	int error, n;
  do{
    printf("Input a positive integer:  ");
    fgets(line, MAXLINE, stdin);
              error = sscanf(line, "%d",&n) !=1||n<=0;
    if (error)

      printf("\nERROR: Do it again.\n");
  } while (error);

athus doesnot catch all the errors. For example 12X in typed instead of 123, the error is not caught. So I have to modify this code to fix this problem.

Actually, you don't have an error as far as sscanf() is concerned. It converted what it could and stopped and the first non-digit character. I did some testing, and sscanf() will not see "23x" as an error because the '23' returns the value 1 and 'n' is 23. The 'x' is simply the character that determins the end of the number.

You might try
CPP / C++ / C Code:
error = sscanf(line, "%d%c",&n, &c);
and check c for whitespace (space,tab,CR,LF) and if it's not you have your error.

Also, your statement
CPP / C++ / C Code:
error = sscanf(line, "%d",&n) !=1||n<=0;
if (error)
can cause problems. It would probably be better to convert this into:
CPP / C++ / C Code:
error = sscanf(line, "%d",&n);
if (error  != 1 || n <= 0)
It's more understandable and accomplishes the same thing.
__________________

Age is unimportant -- except in cheese
  #10  
Old 17-Nov-2004, 04:02
sosy2001 sosy2001 is offline
New Member
 
Join Date: Oct 2004
Posts: 12
sosy2001 is on a distinguished road

I have an error in my code I can't figure it out


This program is written in five different files. I keep getting an error message and I cant figure out what it means. Can you help me please. Here is the error message I get:
ay.obj : error LNK2001: unresolved external symbol _report_a_loss
Debug/prn1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

prn1.exe - 2 error(s), 0 warning(s)

--------------------------------------------------------------------------
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

#define MAXWORD 100

int    get_call_from_user(void);
void   play(int how_many);
void   prn_final_report(int win, int lose, int how_many);
void   prn_instructions(void);
void   report_a_win(int coin);
void   report_a_loss(int coin);
int    toss(void);
----------------------------------------------------------------------
include "heads.h"

int main(void)
{
   char   ans;
   int    no_of_plays;

   printf("\n"
      "THE GAME OF HEADS OR TAILS\n"
      "\n"
      "Do you want instructions?  ");
   scanf(" %c", &ans);
   putchar('\n');
   if (ans == 'y' || ans == 'Y')
      prn_instructions();
   printf("How many times do you want to play?  ");
   scanf("%d", & no_of_plays);
   putchar('\n');
   play( no_of_plays);
   return 0;
}
-------------------------------------------------------------------
#include "heads.h"
  void report_a_win(int coin)
{
	printf("You win, it was a");
	if (coin)
	{
		printf("tail\n");
	}
	else
	{
		printf("head\n");
	}
	return;
}

void report_a_lose(int coin)
{
	printf("You lose, it was a");
	if (coin)
	{
		printf("tail\n");
	}
	else
	{
		printf("head\n");
	}
	return;
}  

int toss(void)
{
   return (rand() % 2);     /* 0 = heads, 1 = tails */
}
-----------------------------------------------------------------------
#include "heads.h"

int get_call_from_user(void)
{
   int   guess;                /* 0 = heads, 1 = tails */

   do {
      printf("Call it:  ");
      if (scanf("%d", &guess) != 1) {
         printf("\nSORRY: Severe input error - bye!\n\n");
         exit(1);
      }
      if (guess != 0 && guess != 1) {
         printf("\n%s\n\n",
            "ERROR: Type 0 for heads, 1 for tails.");
      }
   } while (guess != 0 && guess != 1);
   return guess;
}
-------------------------------------------------------------------
#include "heads.h"

void prn_instructions(void)
{
   printf("%s\n",
      "This is the game of calling heads or tails.\n"
      "I will flip a coin; you call it.  If you\n"
      "call it correctly, you win; otherwise,\n"
      "I win.\n"
      "\n"
      "As I toss the (simulated) coin, I will\n"
      "tell you to \"call it.\"  To call heads,\n"
      "type 0; to call tails, type 1.\n"
      "\n");
}

void prn_final_report(int win, int lose, int how_many)
{
   printf("\n%s\n%s%3d\n%s%3d\n%s%3d\n\n",
      "FINAL REPORT:",
      "   Number of games that you won:  ", win,
      "   Number of games that you lost: ", lose,
      "   Total number of games:         ", how_many);
}
-----------------------------------------------------------------------
Last edited by dsmith : 17-Nov-2004 at 07:03. Reason: Please use [c] & [/c] for syntax highlighting
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Formatting C / C++ code WaltP C Programming Language 1 06-Jan-2008 23:59
getting pWned by this code quasimof C++ Forum 1 21-Oct-2004 22:30
very difficult code - program gaurav_sting C++ Forum 1 16-Jun-2004 00:59
Explain code in MS STL's binary_search rom C++ Forum 11 07-Mar-2004 20:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.