我正在寻求一些问题的帮助。我必须从.txt文件中读取某些“密码”,例如“abE13#”,并进行一些简单的错误检查以确保它们符合某些要求。但是在目前这一刻,它正在打印出密码(这意味着要完成),但它忽略了检查并陷入了打印新行的循环中。我确定它必须对while(ch!='\n')
做一些事情,但我不能确定那里需要什么来代替检查。
ch = inFile.get();
while(!inFile.eof())
{
while(ch != '\n')
{
cout << ch;
if(isalpha(ch))
{
charReq++;
if(isupper(ch))
uppercaseReq++;
else
lowercaseReq++;
}
else if(isdigit(ch))
{
charReq++;
digitReq++;
}
else if(isSpecial(ch))
{
charReq++;
specialCharReq++;
}
if(uppercaseReq < 1)
cout << "\n missing uppercase" << endl;
ch = inFile.get();
}
}
应该遵循这种格式,
Read a character from the password.txt file
while( there are characters in the file )
{
while( the character from the file is not a newline character )
{
Display the character from the file
Code a cascading decision statement to test for the various required characters
Increment a count of the number of characters in the password
Read another character from the password.txt file
}
Determine if the password was valid or not. If the password was invalid,
display the things that were wrong. If the password was valid, display that it
was valid.
Read another character from the file (this will get the character after the
newline character -- ie. the start of a new password or the end of file)
}
Display the total number of passwords, the number of valid passwords, and the
number of invalid passwords
答案 0 :(得分:0)
由于此while(inFile)
,它会保留打印件。这总是如此。将其更改为if语句只是为了检查文件是否已打开:
if ( inFile )
编辑:由于while(ch != '\n')
,它将停在第一个密码处。当他到达第一个密码的末尾时ch
将是'\ n',而失败并停止阅读。将其更改为:
while( !inFile.eof() )
答案 1 :(得分:-1)
while (ch != '\t')
您已将此行伪代码转换为此c ++代码行:
'\t'
'\n'
是制表符,而不是换行符。这肯定会导致问题,为什么你永远不会结束,而只是打印出新的行(真的是EOF,但你没有看到)。
while( !inFile.eof() )
{
while(ch != '\n' && !inFile.eof() )
{
// ...
}
}
是换行符。
试一试。
编辑:
此外,您唯一检查整个ifstream是否错误。我不知道什么时候会发生,但我建议检查EOF标志。你的代码应该变成这样的东西:
package com.example.hp.citysearchapp;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class CityDetailsActivity extends AppCompatActivity {
public static String url;
String getId;
TextView tv;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_details);
tv=(TextView)findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
getId = bundle.getString("gettingId");
url="http://test.maheshwari.org/services/testwebservice.asmx/GetCity?cityId="+getId
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
String citycode = jsonObject.getString("CityCode"); //problem line , no string received here
Toast.makeText(CityDetailsActivity.this,citycode,Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
答案 2 :(得分:-1)
如果你没有检查两次infile,你可能会陷入无限循环。
while(infile.good())
{
while (inFile.good() && ch != '\n')
{
...
}
if (ch == '\n')
{...}
else
{...}
}