Stopping a Loop (C++ Visual Basic)

I am supposed to make loop in which I count the number of characters of a license plate. I can only display the plates when I press 0. My code just outputs the plates once I type the characters. What do I have to do to make the program right?

[code]
#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char plates[15];
ofstream outFile;

outFile.open("plates.txt");


for (int i = 0; i<3; i++)
{
cout<<"Please enter the license plates."<<endl;
cin>>plates;
cin.ignore();

cout<<"The character length is: "<<strlen(plates)<<endl;

outFile<<plates<<endl;
}

outFile.close();

cin.get();
return 0;
}
[code]

This is what the screen outputs, without pressing zero.

Please enter the license plates.
computer
The character length is: 8

Please enter the license plates.
joking
The character length is: 6

Please enter the license plates.
laugh
The character length is: 5


Last edited on
Your program is doing exactly what you're telling it to do.
I see no logic in your program having to do with pressing "0".

Your description of what you want your program to do is not clear.
Do you mean that you want to store the plates in an array, then when all the plates have been entered, enter "0" to signal done, then display all the plates?

Please use code tags correctly. Your closing code tag needs to be
[/code]
.

And what does this have to do with Visual Basic (mentioned in your title)?



Last edited on
visual basic just shows the system I am using, to let everyone know in case they spot something they are not familiar with. My program is supposed to show how many inputs are entered, not the characters. My instructor helped me solve my error. I was supposed to only output the number of inputs once I pressed 0. And thanks for the advice, I had no idea of how to use the codes. I will next time.
visual basic just shows the system I am using

That statement makes no sense. You can't compile C++ in VB. Perhaps you meant you're using Visual Studio.

Because you did not clarify the assignment as requested., I don;t know if this is what you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{   vector<string> plates;
    string plate;
    
    while (true)
    {   cout<<"Please enter a license plate."<<endl;
        cin>>plate;
        if (plate == "0")
            break;
        plates.push_back (plate);              
    }
    for (unsigned int i=0; i<plates.size(); i++)
    {   cout << plates[i] << " is " << plates[i].size() << " characters" << endl;
    }
    system ("pause");
    return 0;
}

Last edited on
Topic archived. No new replies allowed.