{"id":5850,"date":"2022-08-30T23:03:35","date_gmt":"2022-08-30T17:33:35","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/"},"modified":"2022-08-30T23:03:35","modified_gmt":"2022-08-30T17:33:35","slug":"solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/","title":{"rendered":"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-46165046\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"46165046\" data-parentid=\"46164739\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The major problem your code is facing is not about C but about the logical structure of your function. If your <code>emailadd<\/code> contains any character different from <code>@<\/code> and\/or different from <code>.<\/code> you ask for a new email address.<br \/>\nThat&#8217;s not what you want. <\/p>\n<p>Let&#8217;s structure your problem. Here is what you want to do:<\/p>\n<ol>\n<li>Get user input<\/li>\n<li>Check if the input contains an <code>@<\/code><\/li>\n<li>If not, ask for new input<\/li>\n<li>Check if the input contains a <code>.<\/code><\/li>\n<li>If not, aks for new input<\/li>\n<\/ol>\n<p>You don&#8217;t want to do this once, but as long the user input is correct. To be even more precise you want to get a <code>.<\/code> after the <code>@<\/code> (otherwise an email address like <code>stack.overflow@so<\/code> would be valid.<\/p>\n<p>Consider this approach which implements exactly the behaviour defined above:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nint main()\n{\n    char emailadd[50];\n    size_t emailaddL;   \/* Use size_t instead of int *\/\n\n    \/* Get the email address for the first time *\/\n    printf(\"Please enter your email address\\n\");\n    scanf(\"%s\", emailadd);\n    emailaddL=strlen(emailadd);\n\n    \/* Loop while the address has a wrong format *\/\n    while(1){\n        \/* Create flags: 0 character missing, 1 character found *\/\n        int at_flag=0;\n        int dot_flag=0;\n        emailaddL=strlen(emailadd);\n\n        \/* Loop through the char array. You have to loop from 0 to the array lenght -1*\/\n        for(int i=0; i&lt;emailaddL; i++){    \/* You can declare variables in for loops *\/\n\n            \/* If an @ has been found, set the flag *\/\n            if(emailadd[i] == '@'){\n                at_flag=1;\n            }\n            \/* If a . has been found and @ has been found earlier, set the flag *\/\n            if(at_flag &amp;&amp; emailadd[i] == '.'){\n                dot_flag=1;\n            }\n            \/* If both flags have been set, you can stop searching *\/\n            if(at_flag &amp;&amp; dot_flag){\n                break;\n            }\n        }\n\n        \/* If no @ has been found, ask for it *\/\n        if(!at_flag){\n            printf(\"The entered email address is incorrect. Please re-enter your email address and it must contain the '@' specifier\\n\");\n            scanf(\"%s\", emailadd);\n        \/* If an @ has been found, but no ., ask for it *\/\n        } else if(!dot_flag){\n            printf(\"The entered email address is incorrect. Please re-enter your email address and it must contain the '.' specifier, after the '@' specifier\\n\");\n            scanf(\"%s\", emailadd);\n        \/* Otherwise leave the loop *\/\n        } else{\n            printf(\"Everything is fine!\\n\");\n            break;\n        }\n    }\n}\n<\/code><\/pre>\n<p>Let&#8217;s try some scenarios:<\/p>\n<pre><code>$ Please enter your email address\n$ stack.overflow \n$ The entered email address is incorrect. Please re-enter your email address and it must contain the '@' specifier\n$ stack.overflow@so\n$ The entered email address is incorrect. Please re-enter your email address and it must contain the '.' specifier, after the '@' specifier\n$ stack.overflow@so.com\n$ Everything is fine!\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved I want to create a program in C to check for @ and . if typed in by user [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The major problem your code is facing is not about C but about the logical structure of your function. If your emailadd contains any character different from @ and\/or different from . you ask for a new email address. That&#8217;s not what you want. Let&#8217;s structure your problem. Here is what you want to &#8230; <a title=\"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\" aria-label=\"More on [Solved] I want to create a program in C to check for @ and . if typed in by user [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-5850","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] I want to create a program in C to check for @ and . if typed in by user [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The major problem your code is facing is not about C but about the logical structure of your function. If your emailadd contains any character different from @ and\/or different from . you ask for a new email address. That&#8217;s not what you want. Let&#8217;s structure your problem. Here is what you want to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-30T17:33:35+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]\",\"datePublished\":\"2022-08-30T17:33:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\"},\"wordCount\":173,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\",\"name\":\"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-30T17:33:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed] - JassWeb","og_description":"[ad_1] The major problem your code is facing is not about C but about the logical structure of your function. If your emailadd contains any character different from @ and\/or different from . you ask for a new email address. That&#8217;s not what you want. Let&#8217;s structure your problem. Here is what you want to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/","og_site_name":"JassWeb","article_published_time":"2022-08-30T17:33:35+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]","datePublished":"2022-08-30T17:33:35+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/"},"wordCount":173,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/","name":"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-30T17:33:35+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-i-want-to-create-a-program-in-c-to-check-for-and-if-typed-in-by-user-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=5850"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5850\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=5850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=5850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=5850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}