GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Oct-2008, 20:05
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Program to count number of characters


I need to make a program that will get an input from the user and then count the total number of characters in the line. Also this program should, seperatly count out how many numbers , letters, and punctuations the input has.

please help me
Gene Saika
  #2  
Old 11-Oct-2008, 21:16
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: program to count # of characters


Quote:
Originally Posted by genesaika
I need to make a program that will get an input from the user and then count the total number of characters in the line. Also this program should, seperatly count out how many numbers , letters, and punctuations the input has.
The purpose of this site is to provide guidance & help clarify guidance. Given that we have no knowledge about your knowledge, show your work first, & ask specific questions. When you post code, place a [cpp] tag before, & a [/cpp] tag afterwards to both preserve formating & color code all keywords & identifiers.
  #3  
Old 11-Oct-2008, 21:18
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: program to count # of characters


yes that would be my normal request but I have no idea where to start with this
  #4  
Old 11-Oct-2008, 22:18
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: program to count # of characters


Quote:
Originally Posted by genesaika
...I have no idea where to start with this
You can use C-style character strings or instances of the string class with either cin or std::getline(). Regardless of whether you use C-style strings or a string object, access the underlying C-string through operator[], or string::find() to search for specific characters.
  #5  
Old 12-Oct-2008, 16:41
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Program to count number of characters


The best way to do this would probally be to use this:
CPP / C++ / C Code:
int x = strlen(str);
  #6  
Old 12-Oct-2008, 18:29
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Program to count number of characters


Quote:
Originally Posted by lief480
The best way to do this would probally be to use this:
CPP / C++ / C Code:
int x = strlen(str);
Recognize that this can only be used to determine the overall length of a C-style zero-terminated string. The string class has an equivalent as a member function:

http://www.cplusplus.com/reference/s...ng/length.html

Discriminating digits from alphabetic characters can be done through functions found on the following page:

http://www.cplusplus.com/reference/c...e/isdigit.html

...& punctuation can be discerned by functions found on the following page:

http://www.cplusplus.com/reference/c...e/ispunct.html
  #7  
Old 16-Oct-2008, 19:40
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: Program to count number of characters


Thank you all for replying I have made this code with your help but it only counts up to the spaces. When I try to change the char to string I get errors saying I can't do the opperations used in the rest of the code.
Yes I do know that I am using a bunch of headers I prolly don't need i just threw them in to get it to compile.

Here is the code I am using.
CPP / C++ / C Code:
// req4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
#include <windows.h>

using namespace std ;

int main ()
{
  char str[256] ;

  int ca = 0 ;
  int cd = 0 ;
  int cx = 0 ;
  int i = 0 ; 



  cout << "Please enter a sentence.\n" ;
  cin >> str ;

  while (str[i])
  {
    if (ispunct(str[i])) cx++ ;
	if (isalpha(str[i])) ca++ ;
	if (isdigit(str[i])) cd++ ;
    i++ ;

  }
cout << ca << " letter(s) were found.\n" ;
cout << cd << " number(s) were found.\n" ;
cout << cx << " punctuation(s) were found.\n" ;

system ("pause");
  return 0 ;
}

I am sorry I don't have any notes in it yet but I want to get it working right before I do that lol.

please help
Gene Saika
  #8  
Old 16-Oct-2008, 20:22
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Program to count number of characters


Quote:
Originally Posted by genesaika
Yes I do know that I am using a bunch of headers I prolly don't need i just threw them in to get it to compile.
If your goal is to learn C++, cleaning up your code would appear to be in line with your goal.
Quote:
...it only counts up to the spaces.
As stated earlier, you should look into std::getline():

http://www.cplusplus.com/reference/string/getline.html
  #9  
Old 16-Oct-2008, 20:27
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: Program to count number of characters


sorry I just haven't cleaned it up yet lol
That is how I do things make it work then make it look good.
  #10  
Old 16-Oct-2008, 20:33
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Program to count number of characters


Quote:
Originally Posted by genesaika
sorry I just haven't cleaned it up yet lol
Not to belabor the point, but header files are required when function prototypes contained are required somewhere within the source code file. Slapping in header files simply hoping that the result will compile is not learning the system.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.