所以我是C ++的新手。这个想法是它应该为用户提供两个选项,并且任一选项都可以从用户那里读取输入。但是,它们都没有实际读取用户的任何输入,而只是跳到程序的末尾。任何帮助真的很感激!谢谢。
(注意:我知道它与第一个'cin'接受'数字'有关)
#include <stdio.h>
#define SIZE 80
#include <iostream>
int main(void)
{
FILE * pFile;
int c; // variable to hold character input by user
char sentence[SIZE]; // create char array
char filename[SIZE]; //create filename array
int i = 0; // initialize counter i
int number;
std::cout << "Give a number. 1 for file. Anything else for standard.";
std::cin >> number;
std::cin.clear();
if(number==1)
{
puts("Enter filename to append: ");
while ((i < SIZE-1) && (c = getchar()) != '\n') {
filename[i++] = c;}
filename[i]= '\0';
//fgetc(sentence,80,stdin);
pFile=fopen(filename,"a");
puts("Give a sentence to place in file:");
while ((i < SIZE-1) && (c = getchar()) != '\n') {
sentence[i++] = c;}
sentence[i]= '\0';
fputs(sentence,pFile);
fclose(pFile);
do {
c = fgetc (pFile);
} while (c != EOF);
fclose (pFile);
}
else
{
// prompt user to enter line of text
puts("Enter a line of text:");
// use getchar to read each character
while ((i < SIZE-1) && (c = getchar()) != '\n') {
sentence[i++] = c;}
sentence[i]= '\0';
// terminate string
// use puts to display sentence
puts("\nThe line entered was:");
puts(sentence);
}
}
答案 0 :(得分:1)
我认为你必须删除字符&#34;输入&#34;来自stdin。尝试:
TABLE SCHOOL
--------+--------+----
No. |NAME |CLASS |
--------+--------+----
1. |Batman |math |
--------+--------+----
2. |Batman |biology|
--------+--------+----
4. |Batman |karate |
--------+--------+----
5. |Superman|biology|
--------+--------+----
6. |Superman|karate |
--------+--------+----
7. |Superman|swim |
--------+--------+----
TABLE SUPERHERO
--------+-----
No. |NAME |
--------+-----
1. |Batman |
--------+-----
2. |Superman|
--------+-----
TABLE CLASS
--------+-----
No. |CLASS |
--------+-----
1. |Math |
--------+-----
2. |biology |
--------+-----
3. |karate |
--------+-----
4. |Swim |
--------+-----
答案 1 :(得分:1)
编写cin.ignore(numeric_limits&lt; streamsize&gt; :: max(),&#39; \ n&#39;); 代替std :: cin.clear()会丢弃输入流中的所有内容,包括换行符。
#include <stdio.h>
#include <iostream>
#include <ios> // for <streamsize>
#include <limits> // for numeric_limits
#define SIZE 80
using namespace std;
int main(void) {
FILE * pFile;
int c; // variable to hold character input by user
char sentence[SIZE]; // create char array
char filename[SIZE]; //create filename array
int i = 0; // initialize counter i
int number;
std::cout << "Give a number. 1 for file. Anything else for standard.";
std::cin >> number;
//std::cin.clear();
cin.ignore(numeric_limits < streamsize > ::max(), '\n');
if (number == 1) {
puts("Enter filename to append: ");
while ((i < SIZE - 1) && (c = getchar()) != '\n') {
filename[i++] = c;
}
filename[i] = '\0';
//fgetc(sentence,80,stdin);
pFile = fopen(filename, "a");
puts("Give a sentence to place in file:");
while ((i < SIZE - 1) && (c = getchar()) != '\n') {
sentence[i++] = c;
}
sentence[i] = '\0';
fputs(sentence, pFile);
fclose(pFile);
do {
c = fgetc(pFile);
} while (c != EOF);
fclose(pFile);
} else {
// prompt user to enter line of text
puts("Enter a line of text:");
// use getchar to read each character
while ((i < SIZE - 1) && (c = getchar()) != '\n') {
sentence[i++] = c;
}
sentence[i] = '\0';
// terminate string
// use puts to display sentence
puts("\nThe line entered was:");
puts(sentence);
}
}