Please Heelp me ... I gave up

This is a discussion on Please Heelp me ... I gave up within the C++ Programming forums, part of the General Programming Boards category; I know it is a simple mistake but I can't see it ... You know when somebodays brain stoped..... this ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Question That is what ended up tooo ...on more step

    I know it is a simple mistake but I can't see it ...
    You know when somebodays brain stoped.....

    this is the file ...

    #include<iostream>
    #include<string.h>

    int main()
    {
    string str1;
    int i=0;
    int j=0;
    int counterwords =1;

    cout<< "Please input your text string... \n";
    cin>> str1;
    while (str1[i]<>0)
    {
    i++;
    if (str1[i] == " ")
    {
    counterwords++;// how many words we have here
    }
    if (str1[i] <>( ' '|| ',' || ';' || '.' ||'!' || '?' ))
    {
    j++;// how many charactors that the lines have
    }
    }// while

    int ave =0;
    ave = j div counterwords ;// what is the avarage of the words in this line...

    cout<< " the number of words that you have intered is :" << counterwords<<endl;
    cout<< "the number of the char that you have is :"<< j<< endl;
    cout<< " the avarage of the word lingth is :" << ave << endl;

    return 0;

    }

    _____________
    Please chick it up/
    I am drank

    Thnx
    Last edited by NANO; 04-19-2002 at 07:18 PM.
    C++
    The best

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    while (str1[i]<>0)
    Not sure I know what this mean "<>". Do you mean "not equal"? That is writen like this in C++ str1[i] != 0

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I ended up to get this ...

    #include<iostream.h>
    #include<string>

    void main()
    {
    string str1;
    int i=0;
    int j=0;
    int counterwords =1;

    cout<< "Please input your text string... \n";
    cin>> str1;
    while (str1[i] != 0)
    {
    i++;
    if (str1[i] == " ")
    {
    counterwords++; // how many words we have here
    }
    if (str1[i] != ( ' '|| ',' || ';' || '.' ||'!' || '?' ))
    {
    j++;// how many charactors that the lines have
    }
    }// while

    int ave =0;
    ave = j/ counterwords ;// what is the avarage of the words in this line...

    cout<< " the number of words that you have intered is :" << counterwords<<endl;
    cout<< "the number of the char that you have is :"<< j<< endl;
    cout<< " the avarage of the word lingth is :" << ave << endl;
    cout<< endl;
    cout<< endl;
    cout<< "take care";
    //return 0;

    }

    ___________________________
    I ended up to get this error....
    Look at this :
    [NANO@****] (~) g++ counter.cpp -o counter
    counter.cpp: In function `int main (...)':
    counter.cpp: 16: ISO C++ forbids comparison between pointer and integer
    [NANO@****] (~) vi counter.cpp
    _______________
    That was the message...
    I know it is so stioped... but I gave up

    new Drunker.....
    C++
    The best

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Ok next time I have to format your code or change void main() to int main() I will not help you

    Code:
    #include<iostream.h> 
    #include<string> 
    
    int main() 
    { 
    	char str1[44]; 
    	int i=0; 
    	int j=0; 
    	int counterwords =1; 
    
    	cout<< "Please input your text string... \n"; 
    	cin>> str1; 
    	while (str1[i] != 0) 
    	{ 
    		i++; 
    		if (str1[i] == ' ') 
    		{ 
    		counterwords++; // how many words we have here 
    		} 
    
    		if (str1[i] != ( ' '|| ',' || ';' || '.' ||'!' || '?' )) 
    		{ 
    			j++;// how many charactors that the lines have 
    		} 
    	}// while 
    
    	int ave =0; 
    	ave = j/ counterwords ;// what is the avarage of the words in this line... 
    
    	cout<< " the number of words that you have intered is :" << counterwords<<endl; 
    	cout<< "the number of the char that you have is :"<< j<< endl; 
    	cout<< " the avarage of the word lingth is :" << ave << endl; 
    	cout<< endl; 
    	cout<< endl; 
    	cout<< "take care"; 
    
    	return 0; 
    }
    This does stil not give you the right answears but thats a logical thing with your algoritm. Think about it for awhile

  5. #5
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Just glancing at it, I noticed these:
    Code:
    while (str1[i] != 0)  // You are comparing string to integer
    I think you meant to put:
    Code:
    while (str1[i] != '\0')
    ..

    Also I saw this:
    Code:
    if (str1[i] == " ")   // Comparing to an empty string
    You want

    Code:
    if (str1[i] == ' ')
    Hope that helps.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    >>Just glancing at it, I noticed these:

    while (str1[i] != 0) // You are comparing string to integer<<

    I am not 100% sure but I actually think that is valid. I think I have seen this way to check a string in some books.

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Its valid b/c ASCII 0 is NULL. It just isn't very readable. That's why I suggested the other way.

    Thanks for pointing that out though

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Can you mix the old style headers (.h) and the newer style in one program? If so, why do it?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,688
    > if (str1[i] != ( ' '|| ',' || ';' || '.' ||'!' || '?' ))
    This does NOT compare str1[i] with each of those characters

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    First Why.... Why1...

    What is the wrong if I write :

    void main ()
    {
    ----
    ---
    }


    and if I wrote
    int main ()
    {
    -----------
    -----------
    ----------
    return 0;
    }

    I thougt that will be the same ... is it ... ???
    C++
    The best

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Nothing personal Nano, but this is a tiresome point in the forum. There are plenty of threads and FAQ's on this, although I know some of Microsoft's MSDN uses this, probably from C as opposed to C++.
    Always use int main() and return 0. This is the official standard (by the C++ standards committee), and there's a reason for it. Returning 0 lets the OS or whatever called the program know that it ended successfully. void main() compiles ok, but if there's a problem, there's no control over what happens when the program ends.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    OK... I will

    I will do that starting now.....
    int main()
    {
    cout<<" thanks... ";
    return 0;

    }
    C++
    The best

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Last Edition... with some mistakes

    Actually this program is to count how many words do you have in the string str1 and what is the avarage of the words....
    after I read alot about strings and after I got some ideas from here... I would like you to help me fixing the problem .....
    I think it can't count more than one word....
    Please take a look at the progam now....
    #include<iostream.h>
    #include<string>

    int main()
    {
    string str1;
    int j=0;
    int counterwords =1;

    cout<< "Please input your text string... \n";
    cin>> str1;
    cout<< "it is in \n";
    string::iterator str1_ptr = str1.begin();
    int i=0;
    while (str1_ptr <= str1.end())'
    {
    i++; // increase one visit...
    if (*str1_ptr == ' ')
    {
    counterwords++; // how many words we have here
    }
    // if (*str1_ptr != ( ' '|| ',' || ';' || '.' ||'!' || '?' ))
    if (*str1_ptr != ','|| *str1_ptr != ' ')
    {
    j++;// how many charactors that the lines have
    }
    str1_ptr++;
    }// while
    int ave =0;
    j--;// to shift j one because it has 1more char

    ave = j/ counterwords ;// what is the avarage of the words in this line...
    cout<< " the number of words that you have intered is :" << counterwords<<endl;
    cout<< "the number of the char that you have is :"<< j<< endl;
    cout<< " the avarage of the word lingth is :" << ave << endl;
    cout<< endl;
    cout<< "take care"<<endl;
    cout<<" the program inter the loop for :"<<i << " times";
    return 0;
    }
    ______________
    I will go to chick the resultes then I will past them...
    C++
    The best

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    This is the resulte....

    [NANO@****] (~) ./counter
    Please input your text string...
    this is my c++ program, I need help
    it is in
    the number of words that you have intered is :1
    the number of the char that you have is :4
    the avarage of the word lingth is :4

    take care
    the program interd the loop for :5 times
    ---------------
    that is not fare
    Can you please chick the while loop for me...
    C++
    The best

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Please read the last two Reply...

    I just posted on them the latest ver. of my program and the resulte that I got out.... and I don't know why...


    Please help me out....

    thank you....
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Othello game, gave up debugging
    By Nutshell in forum Game Programming
    Replies: 31
    Last Post: 01-26-2003, 12:05 PM
  2. Programs run in dos. Why not windows?
    By marcusbankuti in forum C++ Programming
    Replies: 15
    Last Post: 08-11-2002, 07:33 PM
  3. gave it shot...it gave me an error!
    By LonelyPlanderWa in forum C Programming
    Replies: 3
    Last Post: 07-12-2002, 01:26 AM
  4. Linked list; heelp
    By ylph02 in forum C Programming
    Replies: 1
    Last Post: 05-29-2002, 09:22 PM
  5. I am confused with this code that somebody gave me
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 11-16-2001, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21