Connecting Tech Pros Worldwide Forums | Help | Site Map

C++ Program to output type of input entered (int, char, etc...)

Newbie
 
Join Date: Sep 2007
Posts: 5
#1: Sep 23 '07
i am beginner in using turbo c++. and i want to finish my coding. but then i have no idea on how can i make it.

my program is about if u enter a number it will print "u enter numeric", then if u enter a word the output would be "u enter a string" then last if u enter both numeric and letter the output would be " u enter a variable".

hope i could get help to someone who really have an idea about the program.
my warmest thanks to someone who could give me some part of the codings.......

Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#2: Sep 23 '07

re: C++ Program to output type of input entered (int, char, etc...)


Quote:

Originally Posted by jurmav

HELP!!!!! i am beginner in using turbo c++. and i want to finish my coding. but then i have no idea on how can i make it.

[b]my program is about if u enter a number it will print "u enter numeric", then if u enter a word the output would be "u enter a string" then last if u enter both numeric and letter the output would be " u enter a variable".

hope i could get help to someone who really have an idea about the program.
my warmest thanks to someone who could give me some part of the codings.......

All you need to do is to accept input as string,and then if whole string contains only numbers then it's a numeric,if it only contain letters then it is a word,otherwise if it contains letter and numeric it's a variable.

If you get stuck with coding please post what have you tried and we will try to help you.

Savage

Savage
Newbie
 
Join Date: Sep 2007
Posts: 5
#3: Sep 23 '07

re: C++ Program to output type of input entered (int, char, etc...)


Quote:

Originally Posted by Savage

All you need to do is to accept input as string,and then if whole string contains only numbers then it's a numeric,if it only contain letters then it is a word,otherwise if it contains letter and numeric it's a variable.

If you get stuck with coding please post what have you tried and we will try to help you.

Savage

Savage

the i have here is:

#include<stdio.h>
#include<string.h>
#define p printf
main()
{char a;

clrscr();
p("input a word: ");
scanf("%s",a);
printf("\n you entered a character");
getch();
}

i don't know what will be the next step.
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#4: Sep 23 '07

re: C++ Program to output type of input entered (int, char, etc...)


Quote:

Originally Posted by jurmav

the i have here is:

#include<stdio.h>
#include<string.h>
#define p printf
main()
{char a;

clrscr();
p("input a word: ");
scanf("%s",a);
printf("\n you entered a character");
getch();
}

i don't know what will be the next step.

This is no good.

instead of a single char you need a array of chars.

There are two types of arrays in c:stack arrays and dynamic arrays.

Size of stack array cannot be changed during execution,which is not true for dynamic arrays.To build a dynamic array(you don't know size of the string that user will enter) you just declare a pointer to a char:

char *a.*b;/*You will need 2 arrays

If you want to capture a whole sentence not just a part of the string(scanf breakes on a white space) you use gets() to read in a string.After that you can use strlen to determine the size of first array and the allocate second one:

b=(char*)malloc(sizeof(char)*(strlen(a)+1))

Now you can use as a simple stack based arrays.When you have done this post again.

PS:It's recomended that you read some tutorials about arrays.

Savage
Newbie
 
Join Date: Sep 2007
Posts: 5
#5: Sep 25 '07

re: C++ Program to output type of input entered (int, char, etc...)


Gud day! i quited on the program that i was doing about looking for if the input was variable,string or numeric.
this time i am doing a new program.This is all about the entering of a fname,lname,midname and the subject& section of the student i used an array and.where in i have to use a method in looking out the output or the record.
the output would be like this.
method 1: fname lname midname sub/sect

method II: fname|lname|midname|sub/sect

method III: number of character entered |fname|lname|midname|sub/sect

method IV: fname|lname|midname|sub/sect number of character entered

method V:fname|lanme|midname|sub/sect #

my problem is from method 3 and 4.
i have finished on my codings but then.
the fname,lname,midname and the sub/section are not being read by my strlen commands.
this is what my codings are:

printf(""%d", strlen(whois[x].fname+ " "+ whois[x].lname+" "+whois[x].midname+" "+ whois[x].ss));


the error was : Invalid pointer addition in function method.
hope i can get a response here.
thanks in advance.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: Sep 25 '07

re: C++ Program to output type of input entered (int, char, etc...)


Quote:

Originally Posted by jurmav

Gud day! i quited on the program that i was doing about looking for if the input was variable,string or numeric.
this time i am doing a new program.This is all about the entering of a fname,lname,midname and the subject& section of the student i used an array and.where in i have to use a method in looking out the output or the record.
the output would be like this.
method 1: fname lname midname sub/sect

method II: fname|lname|midname|sub/sect

method III: number of character entered |fname|lname|midname|sub/sect

method IV: fname|lname|midname|sub/sect number of character entered

method V:fname|lanme|midname|sub/sect #

my problem is from method 3 and 4.
i have finished on my codings but then.
the fname,lname,midname and the sub/section are not being read by my strlen commands.
this is what my codings are:

printf(""%d", strlen(whois[x].fname+ " "+ whois[x].lname+" "+whois[x].midname+" "+ whois[x].ss));


the error was : Invalid pointer addition in function method.
hope i can get a response here.
thanks in advance.

Ok!
This is C++ so you are permitted to add two char arrays.
If you like then call strcat or try to do Operator Overloading.
Good Luck :-)

Kind regards,
Dmjpro.
Newbie
 
Join Date: Sep 2007
Posts: 5
#7: Sep 26 '07

re: C++ Program to output type of input entered (int, char, etc...)


Re: C++ Program to output type of input entered (int, char, etc...)
Gud day! i quited on the program that i was doing about looking for if the input was variable,string or numeric.
this time i am doing a new program.This is all about the entering of a fname,lname,midname and the subject& section of the student i used an array and.where in i have to use a method in looking out the output or the record.
the output would be like this.
method 1: fname lname midname sub/sect

method II: fname|lname|midname|sub/sect

method III: number of character entered |fname|lname|midname|sub/sect

method IV: fname|lname|midname|sub/sect number of character entered

method V:fname|lanme|midname|sub/sect #

my problem is from method 3 and 4.
i have finished on my codings but then.
the fname,lname,midname and the sub/section are not being read by my strlen commands.
this is what my codings are:

printf(""%d", strlen(whois[x].fname+ " "+ whois[x].lname+whois[x].midname+ whois[x].ss));


the error was saying: Invalid pointer addition in function method.
hope i can get a response here.
thanks in advance.
Reply