In class my teacher grades our assignment with Turbo C++ the old version like the DOS one just wondering
In class my teacher grades our assignment with Turbo C++ the old version like the DOS one just wondering
Yes, it should work. However, the teacher may not understand it !
--
Mats
It's not a good scanf, because it relies on ANSI or other charsets that are similar.
It also lacks any type to read.
Will it be used to read strings?
yes It will be?? I did so I could put space in the Word
You failed to specify the "s", the buffer size and relied on typical ANSI charsets.
More info and solutions here:
https://apps.sourceforge.net/mediawi...tle=Scanf_woes
Will it work tho???
I'm tempted to say... "no".
Do it right or not at all.
Define "work"... It is unlikely that Turbo C will work with anything other than ANSI/ASCII characters in it's standard C library functions. Then it comes to "what happens if the user types in 1000 characters when you wanted 30", which is a "fault tolerance matter", and it may not be part of your assignment to make your code "resilient to abuse/misuse".
--
Mats
This is an example from Turbo C/C++ version 1.01's help menu. Note the block of code in blue.
Note that flushing stdin has been deprecated, as has conio.h. Your first z in the scanf() call is lowercase, and should be uppercase.Code:#include <stdio.h>
#include <conio.h>
int main(void)
{
char label[20];
char name[20];
int entries = 0;
int loop, age;
double salary;
struct Entry_struct
{
char name[20];
int age;
float salary;
} entry[20];
/* Input a label as a string of characters restricting to 20 characters */
printf("\n\nPlease enter a label for the chart: ");
scanf("%20s", label);
fflush(stdin); /* flush the input stream in case of bad input */
/* Input number of entries as an integer */
printf("How many entries will there be? (less than 20) ");
scanf("%d", &entries);
fflush(stdin); /* flush the input stream in case of bad input */
/* input a name restricting input to only letters upper or lower case */
for (loop=0;loop<entries;++loop)
{
printf("Entry %d\n", loop);
printf(" Name : ");
scanf("%[A-Za-z]", entry[loop].name);
fflush(stdin); /* flush the input stream in case of bad input */
/* input an age as an integer */
printf(" Age : ");
scanf("%d", &entry[loop].age);
fflush(stdin); /* flush the input stream in case of bad input */
/* input a salary as a float */
printf(" Salary : ");
scanf("%f", &entry[loop].salary);
fflush(stdin); /* flush the input stream in case of bad input */
}
/* Input a name, age and salary as a string, integer, and double */
printf("\nPlease enter your name, age and salary\n");
scanf("%20s %d %lf", name, &age, &salary);
/* Print out the data that was input */
printf("\n\nTable %s\n",label);
printf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
printf("-----------------------------------------------------\n");
for (loop=0;loop<entries;++loop)
printf("%4d | %-20s | %5d | %15.2lf\n",
loop + 1,
entry[loop].name,
entry[loop].age,
entry[loop].salary);
printf("-----------------------------------------------------\n");
return 0;
}
What matsp means is that you can specify a maximum length for the input in scanf, so that you don't get a buffer overflow:
This way, if the user enters more than 63 characters, that input will be truncated. 64 is for the null terminator \0.Code:char input[64];
scanf("%63[A-Za-z. ]", input);
I don't understand what Elysia means by there not being a conversion specifier, I had thought [ ] is a conversion specifier (for strings). I don't know where you would stick an "s" in here to make it better...altho unless Adak has hatched some bizarre deception, the OP has nothing to worry about anyway.