prompt user to enter a letter, prnt err if a string is entered.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2475 Views - Last Post: 07 March 2012 - 09:52 AM Rate Topic: -----

#1 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 108
  • Joined: 21-December 11

prompt user to enter a letter, prnt err if a string is entered.

Posted 19 February 2012 - 05:33 PM

I have this codes I want to write: What I want to do is simple I want the program to prompt a user to enter their name
After that, a single character from the alphabet will be entered. The program will then pint out a vowel or consonant based on what the user inputed. If the user input is not a letter between a-z or A-Z or if it is a string of length>1,
The program should print "You did not enter a letter of the alphabet".


#include <iostream>
#include <string>
using namespace std;

int main ()
{

 cout<<"Enter your name please:"<<name<<endl;
 string name;
 getline(cin, name);
 cout << "Enter a letter: "<<alphabet<<endl;
 string alphabet= a-z||A-Z;
 getline(cin,alphabet);
 cout<<"The alphabet entered is:"<<alphabet<<endl;
     // read string until the next separator
     // (space, newline, tab)
     // Or, alternatively:

     // read a whole line into the string name
 if (str.length >1)
 {
     cout << "You did not enter a consonant or vowel,"<< assign default<<endl;
     alphabet = "a-z||A-Z";
 }
 else
 {
     cout << "Thank you, " << name << "for running this simple program!"<< endl;
           }
}




Is This A Good Question/Topic? 0
  • +

Replies To: prompt user to enter a letter, prnt err if a string is entered.

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 19 February 2012 - 08:20 PM

This line has a problem:
cout << "Enter a letter: "<<alphabet<<endl;


you are trying to print a variable alphabet but you have never declared it. You can't use any variable before declaring it, and you can't print a variable before a value has been assigned to it. (Another problem is, why are you trying to print it there anyway?)

This line
string alphabet= a-z||A-Z;

is also wrong. Here you finally seem to be declaring the variable alphabet but you can't assign characters to a string that way. Here you can find the string class constructors.

See if you can figure out the rest.
Was This Post Helpful? 1
  • +
  • -

#3 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 108
  • Joined: 21-December 11

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 21 February 2012 - 07:34 AM


#include <iostream>
#include <string>
using namespace std;

int main ()

{

 string alphabet = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z "||"A, B,
 C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z '\n'";
 alphabet = "a-z||A-Z";
 int len = string alphabet.length();

 cout<<"Enter your name please:"<<name<<endl;
 getline(cin, name);
 cout <<"Welcome:"<<name<<endl;
 cout << "Enter a letter: "<<alphabet<<endl;
 getline(cin,alphabet);
 cout<<"The alphabet entered is:"<<alphabet<<endl;
     // read string until the next separator
     // (space, newline, tab)
     // Or, alternatively:

     // read a whole line into the string name
 if (str.length >1)
 {
     alphabet = "a-z||A-Z";
     cout << "You did not enter a consonant or vowel!"<<name<<endl;

 }
 else
 {
     cout << "Thank you, " << name << "for running this simple program!"<< endl;
           }
}


I have serious problems with strings ...  It is not working for me. Help if you can thanks.



Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 21 February 2012 - 07:40 AM

You can't use an or operator || in an assignment. An assignment has to deal with specific values. You can't tell the compiler "it might be this or it might be that".

You might want to consider using the standard C function isalpha instead.
Was This Post Helpful? 1
  • +
  • -

#5 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 108
  • Joined: 21-December 11

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 22 February 2012 - 01:32 AM

I am not getting anywhere with this... I have all the articles you subscribed but nothing is working. I don't know what to do I need direction here.



#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype>
using namespace std;
int main ()

{

 char string = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z "
 int len=1;
 int len = string alphabet.length();
 cout<<"Enter your name please:"<<name<<endl;
 getline(cin, name);
 cout <<"Welcome:"<<name<<endl;
 cout << "Enter a letter: "<<alphabet<<endl;
 getline(cin,alphabet);
 cout<<"The alphabet entered is:"<<alphabet<<endl;
     // read string until the next separator
     // (space, newline, tab)
     // Or, alternatively:

     // read a whole line into the string name
 if (str.length >1)
 {
     alphabet = "a-z||A-Z";
     cout << "You did not enter a consonant or vowel!"<<name<<endl;

 }
 else
 {
     cout << "Thank you, " << name << "for running this simple program!"<< endl;
           }
}




Was This Post Helpful? 0
  • +
  • -

#6 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 22 February 2012 - 07:11 AM

char string = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z "

This line is full of errors, and you don't even need it. First, it's missing a ;. Second, a char can only hold one character, written between ' '. You also now have both a type and variable named string.

int len=1;
int len = string alphabet.length();

Here you declare the same variable twice, and never use it. What is the word string doing there? Edit: And alphabet isn't declared yet.

cout<<"Enter your name please:"<<name<<endl;

Here you use name before it is declared. Why do you want to output it here?

cout << "Enter a letter: "<<alphabet<<endl;

Same problem as above.

getline(cin,alphabet);

alphabet still isn't declared. Declare it before this line.

if (str.length >1)

str doesn't exist, you want alphabet. Also, length is a function, so add ().

This post has been edited by Hezekiah: 22 February 2012 - 07:13 AM

Was This Post Helpful? 1
  • +
  • -

#7 -Nix-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-December 11

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 05 March 2012 - 02:22 AM

ok im going to take a shot at this, keep in mind im new to programming. So if my syntax is wrong dont be surprised im just tryin to help out here.

 int len=1;
 int len = string alphabet.length();


Your assigning len to the value 1. Then in the next line reassigning it to the length of alphabet. Take out "int len=1" its pointless here.

 cout<<"Enter your name please:"<<name<<endl;

 cout << "Enter a letter: "<<alphabet<<endl;


In these two your using cout, this means you want to output the value of "name" and "alphabet" i believe what you mean is to use cin, cin allows the user to input data. In this case you want them to input the value for name and alphabet. Try this, instead (also with cin use >>, cout uses <<)
 cin >>"Enter your name please:" >> name >>endl;

 cout >> "Enter a letter: " >> alphabet >> endl;



 if (str.length >1)
 {
     alphabet = "a-z||A-Z";
     cout << "You did not enter a consonant or vowel!"<<name<<endl;

 }


In this what you mean is I want to deny anything but letters. Unfortunately "a-z" and "A-Z" do not work. We know what you mean but the computer doesnt. Im still new to programming but i think youll have to something like this ( != means not equal too)

if (alphabet != a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
{
     cout << "You did not enter a consonant or vowel!"<<name<<endl;
}



However im sure there is a better way to do the above code, but im to tired to think lol

This post has been edited by -Nix-: 05 March 2012 - 02:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 158
  • Joined: 05-February 12

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 05 March 2012 - 03:55 AM

@-Nix- - That
if (alphabet != a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
{
     cout << "You did not enter a consonant or vowel!"<<name<<endl;
}



does not work, just so you know.

Besides what everyone told you, you can use
if(alphabet<'a'||alphabet>'Z')
{
    cout << "You did not enter a consonant or vowel!"<<name<<endl;
}



That would be simpler than creating a string with all the alphabet or testing every letter.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 05 March 2012 - 07:13 AM

View Postshurd, on 05 March 2012 - 05:55 AM, said:

Besides what everyone told you, you can use
if(alphabet<'a'||alphabet>'Z')
{
    cout << "You did not enter a consonant or vowel!"<<name<<endl;
}



That would be simpler than creating a string with all the alphabet or testing every letter.

It would be simpler, but it would also be wrong.

alphabet<'a' would rule out all of the uppercase letters, and alphabet>'Z' would rule out all of the lowercase letters. Consult an ascii chart.

But why reinvent the wheel? This is exactly what the isalpha function is for.
Was This Post Helpful? 1
  • +
  • -

#10 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 158
  • Joined: 05-February 12

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 05 March 2012 - 07:29 AM

@r.stiltskin - True, did not took a look at ascii chart before this. I thought they were in order.

The right way would be
if(alphabet<'a'||alphabet>'z')
{
  cout << "You did not enter a consonant or vowel!"<<name<<endl;
}
else if (alphabet<'A'||alphabet>'Z')
{
   cout << "You did not enter a consonant or vowel!"<<name<<endl;
}



or, but I'm not sure if that would work
if((alphabet<'a'||alphabet>'z')&&(alphabet<'A'||alphabet>'Z'))
{
   cout << "You did not enter a consonant or vowel!"<<name<<endl;
}


This post has been edited by shurd: 05 March 2012 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 05 March 2012 - 04:08 PM

View Postshurd, on 05 March 2012 - 09:29 AM, said:

if(alphabet<'a'||alphabet>'z')
{
  cout << "You did not enter a consonant or vowel!"<<name<<endl;
}
else if (alphabet<'A'||alphabet>'Z')
{
   cout << "You did not enter a consonant or vowel!"<<name<<endl;
}


That one will print the error message for any input, because no matter what the user enters, it is either not a lowercase letter (making the "if" condition true) or it is not an uppercase letter (making the "else if" condition true).

View Postshurd, on 05 March 2012 - 09:29 AM, said:

if((alphabet<'a'||alphabet>'z')&&(alphabet<'A'||alphabet>'Z'))
{
   cout << "You did not enter a consonant or vowel!"<<name<<endl;
}


That one looks correct.

But did you try either one?

This post has been edited by r.stiltskin: 05 March 2012 - 04:10 PM

Was This Post Helpful? 0
  • +
  • -

#12 Criftik  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 3
  • Joined: 06-March 12

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 06 March 2012 - 04:39 PM

I'm just gonna go through this and pick it apart a little bit in case this guy comes back.


#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype>
using namespace std;
int main ()

{

 char string = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z "
 int len=1;
 int len = string alphabet.length();
 cout<<"Enter your name please:"<<name<<endl;

 getline(cin, name); // Alright, you've got this mixed around.  Should be cin.getline(name, ) and after the comma you should have the number of characters you want to take.

 cout <<"Welcome:"<<name<<endl; //As others have said, you did not declare your "name" variable.  

 cout << "Enter a letter: "<<alphabet<<endl; //Did not declare this one either.

 getline(cin,alphabet); //Unnecessary, even if it was right.  Since it's just a letter that you want, just use cin >> alphabet;
//Also, you need to declare your variable.  char alphabet; would be acceptable (at the beginning of the program).

 cout<<"The alphabet entered is:"<<alphabet<<endl;
 

/*
* This whole next block is just confusing.  I'm a little too shot from exams to help you here right now.  
* Just know that "alphabet = a-z||A-Z" is not correct.  You cannot use an operator like || in an assignment, and you
* also cannot check all values in between to things by using a dash (-).  "a-z" to the computer would be "VARIABLE(a)
* MINUS VARIABLE(z).  And since you have not declared either variable, you'd get errors thrown at you.  Even if you had
* declared them, the program would not do what you want.  At all.  Ever.
 if (str.length >1)
 {
     alphabet = "a-z||A-Z";
     cout << "You did not enter a consonant or vowel!"<<name<<endl;

 }
 else
 {
     cout << "Thank you, " << name << "for running this simple program!"<< endl;
           }

//The end of every program should have "return 0;".  Insert that here.
}



Comments in the code. On request, I will post a working program that does what you want.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 06 March 2012 - 05:01 PM

View PostCriftik, on 06 March 2012 - 06:39 PM, said:

Comments in the code. On request, I will post a working program that does what you want.

You are requested NOT to post the program. That's not what DIC is about.
Was This Post Helpful? 0
  • +
  • -

#14 Criftik  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 3
  • Joined: 06-March 12

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 06 March 2012 - 10:04 PM

View Postr.stiltskin, on 06 March 2012 - 05:01 PM, said:

You are requested NOT to post the program. That's not what DIC is about.


My bad; forgot/overlooked. Honest mistake, mate.
Was This Post Helpful? 0
  • +
  • -

#15 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 108
  • Joined: 21-December 11

Re: prompt user to enter a letter, prnt err if a string is entered.

Posted 07 March 2012 - 01:32 AM

View PostCriftik, on 06 March 2012 - 04:39 PM, said:

I'm just gonna go through this and pick it apart a little bit in case this guy comes back.


#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype>
using namespace std;
int main ()

{

 char string = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z "
 int len=1;
 int len = string alphabet.length();
 cout<<"Enter your name please:"<<name<<endl;

 getline(cin, name); // Alright, you've got this mixed around.  Should be cin.getline(name, ) and after the comma you should have the number of characters you want to take.

 cout <<"Welcome:"<<name<<endl; //As others have said, you did not declare your "name" variable.  

 cout << "Enter a letter: "<<alphabet<<endl; //Did not declare this one either.

 getline(cin,alphabet); //Unnecessary, even if it was right.  Since it's just a letter that you want, just use cin >> alphabet;
//Also, you need to declare your variable.  char alphabet; would be acceptable (at the beginning of the program).

 cout<<"The alphabet entered is:"<<alphabet<<endl;
 

/*
* This whole next block is just confusing.  I'm a little too shot from exams to help you here right now.  
* Just know that "alphabet = a-z||A-Z" is not correct.  You cannot use an operator like || in an assignment, and you
* also cannot check all values in between to things by using a dash (-).  "a-z" to the computer would be "VARIABLE(a)
* MINUS VARIABLE(z).  And since you have not declared either variable, you'd get errors thrown at you.  Even if you had
* declared them, the program would not do what you want.  At all.  Ever.
 if (str.length >1)
 {
     alphabet = "a-z||A-Z";
     cout << "You did not enter a consonant or vowel!"<<name<<endl;

 }
 else
 {
     cout << "Thank you, " << name << "for running this simple program!"<< endl;
           }

//The end of every program should have "return 0;".  Insert that here.
}



Comments in the code. On request, I will post a working program that does what you want.



I am asking for a working program this one is not helping too many feedbacks but still no way out.
Was This Post Helpful? -1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2