Need help im stuck.

Jul 5, 2016 at 9:27am
Hey guys!i just started learning c++ programming and i am stuck on a problem.
The problem is I have to implement selection construct and continue the loop and i am not sure where to put my continue; at if i want the user to re-type his/her name. Thanks for helping :)

#include <stdio.h>
#include <conio.h>
#define MAX 25

char welcomeMsg[] = "Please enter your name without * or #";
char errorMsg[] = "Error please re - enter your name without * or #"; //error message
void main(void)
{
int j;
char name[MAX];
char selection;
char input;
j = 0;
puts(welcomeMsg);

do {


input= _getch();
if (input == '*' || input == '#')
{

puts(errorMsg);
j = 0;
printf("do you wish to continue? y/n \n");
fflush(stdin);
scanf_s("%c", &selection);




switch (selection)
{
case'y':
case'Y':
puts(welcomeMsg);


break;
case'n':
case'N':
printf("exiting programme\n");

break;
default:printf("Wrong key brah\n");
}




}

gets_s(name, 24);

j++;

fflush(stdin);


continue;
} while (name < 25 && name != " ");

name[j] + 0;
puts("\nYour name is");
printf("%s\n", name);


}


Jul 5, 2016 at 10:29am
Hi,

It is pretty much C code. Inputting a complete name with _getch() may be difficult. That's why functions for inputting a large number of characters at a time exist for that purpose (cin, scanf, etc), and you really should use them instead.

For checking if there is an invalid character in your name, use strchr (at least it's basic)
Jul 5, 2016 at 10:30am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include <stdio.h>
#include <conio.h>

#define MAX 250
using namespace std;

char welcomeMsg[] = "Please enter your name without * or # : ";

char errorMsg[] = "Error please re - enter your name without * or #\n\n";
 
int main(void)
{
    char name[MAX];
    char selection;

    do
    {
        puts(welcomeMsg);
        cin >> name;

        if(strchr(name, '*') || strchr(name, '#'))
        {
            puts(errorMsg);
            printf("Do you wish to continue? y/n : ");

            cin >> selection ;
            switch(selection)
           {
                case 'y' : case 'Y ': continue;
                case 'n' : case 'N' : printf("Exiting programme..."); return 0;
                default : printf("Wrong key brah\n"); continue;
            }
        }
    } while (strlen(name) <= 1);

    puts("\nYour name is ");
    printf("%s\n", name);

    return 0;
}
Last edited on Jul 5, 2016 at 10:55am
Jul 5, 2016 at 10:30am
If you have something u don't understand just ask us :)

Have a good day ~ :)
Jul 5, 2016 at 10:43am
Thanks so much guys <3 <3
Jul 5, 2016 at 10:52am
Glad it helped :)
Jul 5, 2016 at 12:44pm
VRGaming2 wrote:
1
2
3
    char name[MAX];
    ...
        cin >> name; 


I've seen you give this advice twice now. This is a thing you should never do. Use a C++ string, or, at the very least, limit the amount of characters extracted by cin. You're just asking for a buffer overflow here.

Jul 5, 2016 at 12:58pm
I learned from that.
1
2
std::string name;
cin >> name;


:)
Topic archived. No new replies allowed.