Thread: count i++ not working corectly Help

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    count i++ not working corectly Help

    This compiles w/o errors but I want it to tell me how many adults, teens, child and infant were entered. Right now, its only giving me a count of the number of functions. What am I doing wrong?

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    const int SIZE = 50;
    int PeopleTypes[SIZE];
    int count = 0, i = 0,infant = 0, child = 0,teen = 0, adult = 0;
    
    do
    {
    
    cout<< "Enter a number: ";
    cin >>PeopleTypes[i];
    
    
    if (PeopleTypes[i]==1 )
    cout <<"infant"<<i++<< endl;
    else if (PeopleTypes[i]==2)
    cout << " Child"<< i++ << endl;
    else if (PeopleTypes[i]==3)
    cout << "Teenager"<<i++<< endl;
    else if (PeopleTypes[i]==4)
    cout <<"adults at the school function"<<i++<<endl;
    else
    cout << "Invalid Entry!"<<endl;
    
    }
    while (PeopleTypes[i-1]>0);
    i++;
    	
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The i++ needs to be inside the loop, and you should be incrementing the appropriate counter in the if/else if statements instead of i.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You need to indent your code too, it makes it easier to read maintain and debug

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    I made corrections and tried different ways of writing the count, but now it does nothing. What am I missing?

    Code:
    #include <iostream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    const int SIZE = 50;
    	int PeopleTypes[SIZE];
    	int count = 0, i = 0,infant = 0, child = 0,teen = 0, adult = 0;
    
    do
    {
    
    cout<< "Enter a number: ";
    cin >>PeopleTypes[i];
    
    
    if (PeopleTypes[i]==1 )
    cout <<"infant"<<infant++<< endl;
    	else if (PeopleTypes[i]==2)
    cout << " Child"<< child++ << endl;
    	else if (PeopleTypes[i]==3)
    cout << "Teenager"<<teen++<< endl;
    	else if (PeopleTypes[i]==4)
    cout <<"adults at the school function"<<adult++<<endl;
    	else
    cout << "Invalid Entry!"<<endl;
    
    }
    while (PeopleTypes[i-1]>0);
    cin.ignore();	
    
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    but now it does nothing.
    whats 'nothing'? since its a 'do' loop it should 'do' something a minimum of one time, if it was just a 'while' loop, then its not guaranteed to loop at all.

    What am I missing?
    you initialize i to 0. now read this: while (PeopleTypes[i-1]>0);

    i - 1 > 0 = true/false
    0 - 1 > 0 = true/false
    -1 > 0 = true/false
    -1 > 0 = false

    then you dont increment i anywhere, so it will run the loop once then exit.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by nadroj
    whats 'nothing'? since its a 'do' loop it should 'do' something a minimum of one time, if it was just a 'while' loop, then its not guaranteed to loop at all.

    you initialize i to 0. now read this: while (PeopleTypes[i-1]>0);

    i - 1 > 0 = true/false
    0 - 1 > 0 = true/false
    -1 > 0 = true/false
    -1 > 0 = false

    then you dont increment i anywhere, so it will run the loop once then exit.
    When I say nothing I mean the cmd window comes up, i type in 1 and the window disappears.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i copied and pasted your code (however i changed main signature to 'int main(void)') and it displays the cout. your not even getting the cout?
    it does, as i mentioned, run once no matter what number you enter then exit. but i dont know why it doesnt work for you.. whats the _tmain thing? my guess is thats the problem, try changing it to the main signature i used (or any standard c++ one)
    Last edited by nadroj; 10-16-2006 at 08:48 PM.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    When I say nothing I mean the cmd window comes up, i type in 1 and the window disappears.
    Try actually opening cmd or some other shell, tryping the full path of your program into the prompt, and hit [Enter]. After the program has finished, there is nothing to keep it from closing immediately, and so it does. Read the FAQ for more info.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    or if you are too lazy to open cmd, juz put
    Code:
    while(1) {}
    just before return 0;


  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by jafet
    Try actually opening cmd or some other shell, tryping the full path of your program into the prompt, and hit [Enter]. After the program has finished, there is nothing to keep it from closing immediately, and so it does. Read the FAQ for more info.
    Thanks for the tip. I added this
    Code:
    std::cout <<"Press [Enter] to continue" <<std::endl;
      std::cin.get();

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by nadroj
    i copied and pasted your code (however i changed main signature to 'int main(void)') and it displays the cout. your not even getting the cout?
    it does, as i mentioned, run once no matter what number you enter then exit. but i dont know why it doesnt work for you.. whats the _tmain thing? my guess is thats the problem, try changing it to the main signature i used (or any standard c++ one)
    Thanks for the tip, that signature is a default or something with the visual studio.net version that I have. I changed it to int main ()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  2. Why is this function not working? Sigh...
    By musique in forum C++ Programming
    Replies: 16
    Last Post: 05-03-2009, 04:54 PM
  3. Sorting function not working
    By musique in forum C++ Programming
    Replies: 13
    Last Post: 05-03-2009, 04:38 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM