r/C_Programming • u/Real-Nebula-2074 • 12h ago
Is this a VS Code newline/paste input bug?
I wrote the following C program in VS Code. Its purpose is to find words that appear in both of two English paragraphs.
(Details: a word must contain at least four letters; each paragraph ends with a \n; the output words should be sorted in ascending order.)
To keep track of how far the program has gotten, I deliberately print stage1 / stage2 after each of the two paragraphs has been read.
But when I run it in the terminal, this is what happens.
First I copy two paragraphs of English text, for example:
In computer science, artificial intelligence (AI), sometimes called machine intelligence, is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Colloquially, the term "artificial intelligence" is used to describe machines that mimic "cognitive" functions that humans associate with other human minds, such as "learning" and "problem solving". From Wiki. Machine learning is a way to achieve artificial intelligence.
I assumed that after pasting, pressing Enter right after intelligence. would produce stage1, and that stage2 would follow right away, printing the words. But in reality I get nothing but a cold, lonely stage1. Pressing Enter again does absolutely nothing. I type a random letter, press Enter once more — and only then do stage2 and the rest of the output finally appear.
I'm a first-year CS student and still not very familiar with programming. I'd like to know: is this a VS Code problem, or is it my problem?
#include <stdio.h>
#include <string.h>
void clean(char (*word)[21], int len) {
for (int i = 0; i <= (len); i++) {
if ((*word)[i] >= 'A' && (*word)[i] <= 'Z') {
(*word)[i] -= 'A' - 'a';
}
}
}
void print(char (*words)[1000][21], int m) {
char temp[21];
char min[21];
int flag = 0;
strcpy(min, "z");
strcpy(temp, "z");
for (int j = 0; j <= m; j++) {
strcpy(min, "z");
for (int i = 0; i <= m; i++) {
if ((strcmp(min, (*words)[i]) >= 0) &&
(strcmp(temp, (*words)[i]) < 0 || flag == 0) &&
((*words)[i] != 0)) {
strcpy(min, (*words)[i]);
}
}
flag = 1;
strcpy(temp, min);
printf("%s\n", min);
}
}
int main(void) {
int r;
char shit[2];
int len1;
int len2;
char para1[1000][21] = {{0}};
char para2[1000][21] = {{0}};
char space;
char answer[1000][21] = {{0}};
for (int i = 0; i < 1000; i++) {
scanf("%[A-Za-z]", ¶1[i][0]);
len1 = i;
scanf("%*[^\nA-Za-z]");
if ((r = scanf("%[\n]", shit)) == 1 || r == EOF) {
printf("stage1\n");
break;
}
}
for (int j = 0; j < 1000; j++) {
scanf("%[A-Za-z]", ¶2[j][0]);
len2 = j;
scanf("%*[^\nA-Za-z]");
if ((r = scanf("%[\n]", shit)) == 1 || r == EOF) {
printf("stage2\n");
break;
}
}
for (int i = 0; i <= len1; i++) {
clean(&(para1[i]), strlen(para1[i]) - 1);
}
for (int j = 0; j <= len2; j++) {
clean(&(para2[j]), strlen(para2[j]) - 1);
}
int m = 0;
int flag = 0;
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if ((strcmp(para1[i], para2[j]) == 0) && (strlen(para1[i]) >= 4)) {
for (int p = 0; p < m; p++) {
if (strcmp(para1[i], answer[p]) == 0) {
flag = 1;
}
}
if (flag == 0) {
strcpy(answer[m++], para1[i]);
}
flag = 0;
}
}
}
m--;
print(&answer, m);
}
2
u/mjmvideos 12h ago
Run it in the debugger and step through it. (Best) or add some print statements to see what’s happening (second best)
2
u/mikeblas 11h ago
Please correctly format your code. It's Rule #1 of this sub.
I'm not sure what you expect of this code:
scanf("%[A-Za-z]", ¶1[i][0]);
or this:
scanf("%*[^\nA-Za-z]");
scanf() doesn't support regular expressions, if that's what you're trying.
Another troubling spot is this declaration
void clean(char (*word)[21], int len)
which takes an array of 21 pointers. That probably menat to be an array of pointers to 21. The code just looks at the first character of each string. What is it meant to do?
But when you call it like this:
for (int i = 0; i <= len1; i++) {
clean(&(para1[i]), strlen(para1[i]) - 1);
}
you're passing the length of one string, and then passing the first pointer within an array of other pointers that ... well, it's such a mess that I'm not really sure what it is you're trying to do.
But, to answer your question: you have provided no evidence of any bug in Visual Studio Code.
1
u/21Ali-ANinja69 4h ago
C
char *par;
scanf("%s", par);
Two scanfs will get you your input.
Never assume how a fuction works. Scanf for a string will go until it hits a newline
-2
u/Own_Unit_6170 9h ago
It's not a bug in VS. I haven't figured out exactly what you are trying to do, but the scanf() are not doing what you seem to be expecting them to do. It's very hard to follow the logic, although program appears to (eventually) give a correct result.
Here is a different (but not necessarily optimal) way to do it:
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
using namespace std;
deque<string> Parse(string);
int main(int iArgs, char* pcArgs[])
{
string sPara1, sPara2;
cout << "Paragraph 1? ";
getline(cin, sPara1);
cout << "Paragraph 2? ";
getline(cin, sPara2);
deque<string> dPtab1=Parse(sPara1);
deque<string> dPtab2=Parse(sPara2);
deque<string> dResult;
if (dPtab1.size()==0 || dPtab2.size()==0)
return 0;
sort(dPtab1.begin(),dPtab1.end());
sort(dPtab2.begin(),dPtab2.end());
dPtab1.erase(unique(dPtab1.begin(),dPtab1.end()),dPtab1.end());
dPtab2.erase(unique(dPtab2.begin(),dPtab2.end()),dPtab2.end());
string sWord1, sWord2;
sWord1=dPtab1.front();
sWord2=dPtab2.front();
while (dPtab1.size()>0 && dPtab2.size()>0)
{
while (sWord1<sWord2)
{
dPtab1.pop_front();
if (dPtab1.size()==0)
break;
sWord1=dPtab1.front();
}
if (sWord1==sWord2)
{
if (sWord1.size()>=4)
dResult.push_back(sWord1);
dPtab1.pop_front();
dPtab2.pop_front();
sWord1=dPtab1.front();
sWord2=dPtab2.front();
}
else
{
while (sWord2<sWord1)
{
dPtab2.pop_front();
if (dPtab2.size()==0)
break;
sWord2=dPtab2.front();
}
}
}
for (string sWord:dResult)
cout << sWord << endl;
}
deque<string> Parse(string sIn)
{
deque<string> dOut;
string sWord="";
for (char cIn:sIn)
{
if ((cIn>='A' && cIn<='Z') || (cIn>='a' && cIn<='z'))
sWord+=cIn|0x20;
else
if (sWord!="")
{
dOut.push_back(sWord);
sWord="";
}
}
if (sWord!="")
dOut.push_back(sWord);
return dOut;
}
2
2
2
u/tastygames_official 12h ago
I didn't read the text but I just had to format your code properly, sorry...