0

Why is it, when I compile this, the cout statement runs twice at first? It does not do that when I have the rest of the code in there that is int. Any thoughts of why or what I am doing wrong..

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

int main()
{ 

    string *name;
  //  int *votes;   
    int i;     
    int numVotes;  

    cout << "how many voters: ";                    
    cin >> numVotes;
    name = new string[numVotes];

    for(i=0;i < numVotes; i++){
        cout <<"enter canidates last names: ";
        getline(cin,name[i]);
    }

    for(i = 0;i < numVotes; i++){
        cout << name[i] << endl; 
    }                                         

    //moved rest of code that's integer and not having this problem out. Basicly
    //same thing, Dynamicly allocated votes of the canidates for a percentage later.

delete [] name;
system ("pause");
return 0;
}
/**************************************************************************************/
// below bit of code (unfinished mind you) does not cout repeat..don't understand.

/*votes = new int[numVotes];                                

    for(int i = 0;i < numVotes; i++){                        
        cout << "enter votes received : ";             
        cin >> votes[i];                                 
    }*/


    /*for(int x=0;x<6;x++){
        total = total + votes[x];   
        perc[x] = (votes[x] * 100) / total;
        cout << "Canidate \t\t\t" << "Votes Received \t\t\t" << "% of Total Votes";
        cout << name[x] << "\t\t\t\t\t" << votes[x] << "\t\t\t\t" <<  perc[x] << endl;
    }*/
/********************************************/

//delete [] votes;

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by gerard4143
0

Try adding this line to your program

while ((ch = cin.get()) != '\n'){}

and read this link to find out what's going on

http://www.daniweb.com/forums/thread90228.html

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

int main()
{
  string *name;
  // int *votes;
  int i;
  int numVotes;
  char ch;

  cout << "how many voters: ";
  cin >> numVotes;

  name = new string[numVotes];
  
  while ((ch = cin.get()) != '\n'){}
  
  for(i = 0; i < numVotes; i++)
  {
    cout <<"enter canidates last names: ";
    getline(cin,name[i]);
  }

  for(i = 0;i < numVotes; i++)
  {
    cout << name[i] << endl;
  }
  
  return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.