当试图用c ++中的文本文件读取时,它给了我一个大的随机数

时间:2017-06-14 17:05:45

标签: c++

我试图制作一个获得你名字的程序并用它来迎接你。这是我的代码:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>

using namespace std;

int main()
{


       ifstream infile;
       infile.open("Info.txt");

int x;

infile >> x;

cout << "Hello " << x << endl;



return 0;
}

我的Info.txt只包含1行&#34; Bob&#34;,但是当我运行程序时,我得到的是#34; Hello 1978627034&#34;

2 个答案:

答案 0 :(得分:0)

您需要将名称作为字符串读取,而不是int:

public class myExpandRecylerAdapter extends ExpandableRecyclerAdapter<TitleParentViewHolder, TitleChildViewHolder> {

LayoutInflater mInflater;
assignment_class myAssignment;
myQueueTask myQueueTask;
Dialog dialog;
Calendar myCalendar;
DatePickerDialog.OnDateSetListener date;
boolean DoneFlag = false;

public myExpandRecylerAdapter(Context context, List<ParentObject> parentItemList) {
    super(context, parentItemList);
    mInflater = LayoutInflater.from(context);
}



@Override
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
    View view = mInflater.inflate(R.layout.list_parent, viewGroup, false);
    return new TitleParentViewHolder(view);
}

@Override
public TitleChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) {
    View view = mInflater.inflate(R.layout.list_child, viewGroup, false);
    return new TitleChildViewHolder(view);
}

@Override
public void onBindParentViewHolder(TitleParentViewHolder titleParentViewHolder, int i, Object o) {
    ParentViewClass title = (ParentViewClass) o;
    titleParentViewHolder.tvParent.setText(title.getTitle());

}

@Override
public void onBindChildViewHolder(final TitleChildViewHolder titleChildViewHolder, int i, Object o) {
    // o instanceof class can be used to determine the class type and template this adapter


    if (o instanceof assignment_class) {
        Log.d("instanceof","assignment_class");
        myAssignment = (assignment_class) o;
        titleChildViewHolder.tvTitle.setText(myAssignment.ass_name);
        titleChildViewHolder.tvTime.setText(myAssignment.TimeLeft());

        titleChildViewHolder.itemView.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                final assignment_class ModifiedAssignment = myAssignment;


                dialog = new Dialog(titleChildViewHolder.itemView.getContext());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.assignment_dialog);
                dialog.show();
                Log.d("onClick", "Child: " + myAssignment.toString());


                // Set the textView at the top to the ass_name to indicate which assignment is being modified
                TextView Name = (TextView) dialog.findViewById(R.id.tvName);
                Name.setText(myAssignment.ass_name);


                Spinner DropDown = (Spinner) dialog.findViewById(R.id.spDropDown);

                // Create an ArrayAdapter using the string array and a default spinner
                ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(titleChildViewHolder.itemView.getContext(), R.array.class_list, android.R.layout.simple_spinner_item);
                // Specify the layout to use when the list of choices appears
                staticAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // Apply the adapter to the spinner
                DropDown.setAdapter(staticAdapter);

                DropDown.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Log.v("item", (String) parent.getItemAtPosition(position));
                        myAssignment.class_id = position;
                        Log.d("Set", myAssignment.class_id.toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                // Initialize for DatePicker
                myCalendar = Calendar.getInstance();

                // Adapter
                date = new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {
                        // TODO Auto-generated method stub
                        myCalendar.set(Calendar.YEAR, year);
                        myCalendar.set(Calendar.MONTH, monthOfYear);
                        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                        //Set textview
                        String myFormat = "MM/dd/yyyy"; //In which you need put here
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                        ((TextView) dialog.findViewById(R.id.tvDate)).setText(sdf.format(myCalendar.getTime()));
                    }

                };


                final TextView etDate = (TextView) dialog.findViewById(R.id.tvDate);


                // Default to today
                String myFormat = "MM/dd/yyyy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                ((TextView) dialog.findViewById(R.id.tvDate)).setText(sdf.format(myCalendar.getTime()));


                // OnClick will open the dialogue for DatePicker
                etDate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new DatePickerDialog(dialog.getContext(), date, myCalendar
                                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                    }
                });


                final TextView etTime = (TextView) dialog.findViewById(R.id.etTime);
                //Default time
                etTime.setText("00:00");


                // OnClick will open the dialogue for TimePicker
                etTime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TimePickerDialog mTimePicker;

                        mTimePicker = new TimePickerDialog(dialog.getContext(), new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                                etTime.setText(selectedHour + ":" + selectedMinute);
                            }
                        }, Calendar.HOUR_OF_DAY, Calendar.MINUTE, false);
                        mTimePicker.setTitle("Select Time");
                        mTimePicker.show();
                    }
                });


                final Button Done = (Button) dialog.findViewById(R.id.bDone);

                Done.setText("NOT DONE");
                Done.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!DoneFlag) {
                            Done.setText("DONE");
                            DoneFlag = true;
                        } else {
                            Done.setText("NOT DONE");
                            DoneFlag = true;
                        }
                    }
                });

                Button Cancel = (Button) dialog.findViewById(R.id.bCancel);
                Button Confirm = (Button) dialog.findViewById(R.id.bConfirm);
                Confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ModifiedAssignment.ass_id = myAssignment.ass_id;
                        ModifiedAssignment.ass_name = ((TextView) dialog.findViewById(R.id.tvName)).getText();
                        ModifiedAssignment.class_id = 0; //TODO: This

                        // We have to convert etDate to Date
                        SimpleDateFormat parser_date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                        try {
                            ModifiedAssignment.due = parser_date.parse(etDate.toString() + " " + etTime + ":00");
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        ModifiedAssignment.date_assigned = myAssignment.date_assigned;
                        ModifiedAssignment.done = ((((Button) dialog.findViewById(R.id.bDone)).getText() == "DONE"));
                        ModifiedAssignment.weight = 99; //TODO: This aswell

                        myQueueItem item = null;
                        try {
                            item = new myQueueItem(new assignment_class(myAssignment, ModifiedAssignment));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        item.setMode("EDIT");
                        myQueue.getInstance().addQueueItem(item);

                    }
                });

                Cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });
            }
        });

    }

    if (o instanceof myQueueTask) {
        myQueueTask = (myQueueTask) o;
        titleChildViewHolder.tvTitle.setText(myQueueTask.toString());
        titleChildViewHolder.tvTime.setText("time");

    }
}


}

答案 1 :(得分:0)

Bob不是int, 将其更改为字符串。