AI, Data Science, & Programming for Small Business Applications & Large Enterprises › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 1: [C] – Data Types, Operators, Conditional Statements, Loops, and Command Line › Do-while code for long integer input
Tagged: do-while loop, get_long, int type, long type
- This topic is empty.
-
AuthorPosts
-
September 9, 2021 at 8:41 am #98
It is mentioned in the problem:
American Express uses 15-digit numbers, MasterCard uses 16-digit numbers, and Visa uses 13- and 16-digit numbers.
So with get_long, I perhaps need to restrict input integers by user from 13 to 16 digits.The objective is met for smaller integers such as between 10 and 99.
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> int main(void) { int input; do { input = get_long("enter"); } while (input<10 || input>99); }
[/dm_code_snippet]
Seeking help for ways I can restrict input of integers from 13 to 16 digit numbers. While I can accept input from 10 to 99, not working when instead of 10, replaced with 1000000000000 and 99 replaced with 9999999999999999.
Reply
Okay after replacing int with long, indeed I could accept input from 13 to 16 digit numbers.
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> int main(void) { long input; do { input = get_long("enter"); } while (input<1000000000000 || input>9999999999999999); }
[/dm_code_snippet]
https://cs50.stackexchange.com/questions/41891/do-while-code-for-long-integer-input[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.